I have a new Mvc Core application. I use jquery to make an ajax call to my controller. I am trying to return an error message in case of an exception in the controller. Here is my code sample:
public async Task<IActionResult> UpdateAllocations([FromBody]AllocationsDto allocationsDto)
{
...
catch (Exception ex)
{
Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
return Json(new { success = false, responseText = ex.Message });
}
...
$.ajax({
type: "POST",
url: "/Allocation/UpdateAllocations",
data: JSON.stringify(allocationData),
contentType: "application/json; charset=utf-8",
dataType: "json"
})
.done(function (data) {
})
.fail(function (xhr, status, error) {
displayError("The ajax call to UpdateAllocations() action method failed:",
error, "Please see the system administrator.");
});
But the error parameter is empty, and status has only one word "error" as well as xhr.statusText. How can I return my own custom text, in this case ex.Message??