我有一个新的Mvc核心应用程序。我使用jquery对我的控制器进行ajax调用。我正在尝试返回一个错误消息,以防控制器中出现异常。下面是我的代码示例:
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.");
});
但是error参数是空的,并且status只有一个单词"error“和xhr.statusText。如何返回自己的自定义文本,在本例中为ex.Message??
https://stackoverflow.com/questions/56550572
复制相似问题