Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何将错误消息返回到Ajax调用_Jquery_Ajax_Asp.net Core Mvc - Fatal编程技术网

Jquery 如何将错误消息返回到Ajax调用

Jquery 如何将错误消息返回到Ajax调用,jquery,ajax,asp.net-core-mvc,Jquery,Ajax,Asp.net Core Mvc,我有一个新的Mvc核心应用程序。我使用jquery对我的控制器进行ajax调用。我试图在控制器出现异常时返回错误消息。以下是我的代码示例: public async Task<IActionResult> UpdateAllocations([FromBody]AllocationsDto allocationsDto) { ... catch (Exception ex) { Resp

我有一个新的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 });

            }
但是error参数为空,status只有一个单词“error”和xhr.statusText。我如何返回自己的自定义文本,在这种情况下,例如消息

类型:函数(jqXHR jqXHR、字符串textStatus、字符串ERRORSHORN) 请求失败时要调用的函数。该函数接收三个参数:jqXHR(在jQuery 1.4.x中,XMLHttpRequest)对象、描述发生的错误类型的字符串和可选的异常对象(如果发生)。第二个参数(除null外)的可能值为“timeout”、“error”、“abort”和“parserror”。当HTTP错误发生时,ErrorSprown接收HTTP状态的文本部分,例如“未找到”或“内部服务器错误”

您需要从第一个参数的responseJSON获取消息

.fail(function (xhr, status, error) {

       displayError("The ajax call to UpdateAllocations() action method failed:",
                    xhr.responseJSON.responseText, "Please see the system administrator.");
});

是这个吗?@gaetanoM谢谢你的建议。不幸的是,该解决方案不适用于MVC核心,它返回HttpStatusCodeResult。MVC核心使用StatusCodeResult,它的构造函数只接受一个参数,即状态代码,并且没有错误消息…我使用了返回状态代码(404,new{message=ex.message});同样的结果。。。
.fail(function (xhr, status, error) {

       displayError("The ajax call to UpdateAllocations() action method failed:",
                    xhr.responseJSON.responseText, "Please see the system administrator.");
});