[英]ASP.NET MVC 5 ajax error statusText is always “error”
I'm having a problem sending a custom message on error to an ajax call. 我在将错误的自定义消息发送到ajax调用时遇到问题。 My controller returns something like this:
我的控制器返回如下内容:
return new HttpStatusCodeResult(400, "My error!");
And my ajax code looks like this: 我的ajax代码如下所示:
error: function (xhr, httpStatusMessage) {
console.log(xhr.statusText);
console.log(httpStatusMessage);
}
The problem is that xhr.statusCode and httpStatusMessage is always "error". 问题是xhr.statusCode和httpStatusMessage始终为“错误”。 What am I doing wrong now?
我现在在做什么错? I'm expecting to have "My error!"
我期望出现“我的错误!” in xhr.statusText.
在xhr.statusText中。
I'm using ASP.NET MVC 5
and jquery-1.10.2
我正在使用
ASP.NET MVC 5
和jquery-1.10.2
My xhr output is: 我的xhr输出是:
abort:ƒ ( statusText )
always:ƒ ()
complete:ƒ ()
done:ƒ ()
error:ƒ ()
fail:ƒ ()
getAllResponseHeaders:ƒ ()
getResponseHeader:ƒ ( key )
overrideMimeType:ƒ ( type )
pipe:ƒ ( /* fnDone, fnFail, fnProgress */ )
progress:ƒ ()
promise:ƒ ( obj )
readyState:4
responseText:"Bad Request"
setRequestHeader:ƒ ( name, value )
state:ƒ ()
status:400
statusCode:ƒ ( map )
statusText:"error"
success:ƒ ()
then:ƒ ( /* fnDone, fnFail, fnProgress */ )
My Web.config httpErrors configuration looks like this: 我的Web.config httpErrors配置看起来像这样:
<httpErrors existingResponse="PassThrough" errorMode="Custom">
<remove statusCode="404" />
<error statusCode="404" path="/Error/NotFound" responseMode="ExecuteURL" />
<remove statusCode="403" />
<error statusCode="403" path="/Error/Forbidden" responseMode="ExecuteURL" />
</httpErrors>
and still, on my dev environment, the responseText is empty and the statusText is just "error". 而且,在我的开发环境中,responseText为空,而statusText只是“错误”。
You need to set a property in your Web.Config file. 您需要在Web.Config文件中设置一个属性。
Citing a user of this web page on github, emphasis mine, 在github上引用此网页的用户,重点是我的,
By default IIS will mask your error codes and replace them with default errors .
默认情况下,IIS将屏蔽您的错误代码并将其替换为默认错误 。 The "PassThrough" option tells IIS to leave your custom errors alone and render them as-is.
“ PassThrough”选项告诉IIS保留您的自定义错误,并按原样呈现它们。
"Bad Request" is the default http error text for the status code 400. “错误请求”是状态码400的默认http错误文本 。
So there is the setting as documented here and outlined here , 因此,这里有此处记录和此处概述的设置,
<configuration>
<system.webServer>
<httpErrors existingResponse="PassThrough"></httpErrors>
</system.webServer>
</configuration>
Consult the documentation carefully for your version of IIS, there is lots of subtle version differences. 仔细查阅文档以了解您的IIS版本,其中存在许多细微的差异。
EDIT 编辑
Not really specific to MVC, but it is how I once solved it (part of production code), and what seems to have helped OP as well: 并不是真正针对MVC,但这是我曾经解决它(生产代码的一部分)的方式,似乎也对OP有所帮助:
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
Response.ContentType = "text/plain";
Response.Write(new String('_', 513) + "my custom message");
This absurd minimum character limit thing may or may not be needed depending on IIS version. 根据IIS版本的不同,可能需要或可能不需要此荒谬的最小字符限制 。 I would too be grateful if somebody could shed a little more light on this underdocumented behavior.
如果有人能进一步阐明这种证据不足的行为,我也将不胜感激。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.