I have an MVC controller (not api controller) [HttpPost]
method public async Task<string> PostDocument()
to which I'm making an ajax call from client. And that method is making another API call to an [HttpPost]
method PostDocument(DocRepoViewModel docRepo)
. Below is my code:
public async Task<string> PostAdminUploadData()
{
HttpResponseMessage response = null;
//set the parameter docRepo (a complex object)
try {
using (var client = new HttpClient())
{
string uri = documentRepositoryApiUrl + Constants.PostDoc;
client.BaseAddress = new Uri(documentRepositoryApiUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
response = await client.PostAsJsonAsync(Constants.PostDoc, docRepo); //docRepo parameter for API method
}
}
catch (Exception ex){}
return (await response.Content.ReadAsAsync<string>());
}
Now in the above code response = await client.PostAsJsonAsync(Constants.PostDoc, docRepo);
code piece returns success
to response var (because its doing what I expected) but when the above method responds, the responds is always caught in error:
in ajax bellow my ajax:
$.ajax({
url: url,
type: 'Post',
data: data,
cache: false,
dataType: 'json',
async: true,
contentType: false,
processData: false,
success: function (data) {
alert("pass");
},
error: function (data) {
alert("field"); //always failed
}
});
There is no exception thrown anywhere c#. Please help me to solve the problem