ASP.NET MVC 5 Ajax错误statusText始终为“错误". [英] ASP.NET MVC 5 ajax error statusText is always "error"

查看:187
本文介绍了ASP.NET MVC 5 Ajax错误statusText始终为“错误".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在向ajax调用发送错误时发送自定义消息时遇到问题.我的控制器返回了以下内容:

I'm having a problem sending a custom message on error to an ajax call. My controller returns something like this:

return new HttpStatusCodeResult(400, "My error!");

我的ajax代码如下:

And my ajax code looks like this:

error: function (xhr, httpStatusMessage) {
              console.log(xhr.statusText);
              console.log(httpStatusMessage);
}

问题在于xhr.statusCode和httpStatusMessage始终为错误".我现在在做什么错? 我期望出现我的错误!"在xhr.statusText中.

The problem is that xhr.statusCode and httpStatusMessage is always "error". What am I doing wrong now? I'm expecting to have "My error!" in xhr.statusText.

我正在使用ASP.NET MVC 5jquery-1.10.2

我的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 */ )

我的Web.config httpErrors配置看起来像这样:

My Web.config httpErrors configuration looks like this:

<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>

在我的开发环境中,responseText为空,statusText只是错误".

and still, on my dev environment, the responseText is empty and the statusText is just "error".

推荐答案

您需要在Web.Config文件中设置属性.

You need to set a property in your Web.Config file.

在github上引用此网页的用户,重点是我的

Citing a user of this web page on github, emphasis mine,

默认情况下,IIS将屏蔽您的错误代码,并将其替换为默认错误. "PassThrough"选项告诉IIS保留您的自定义错误,并按原样呈现它们.

By default IIS will mask your error codes and replace them with default errors. The "PassThrough" option tells IIS to leave your custom errors alone and render them as-is.

错误请求"是状态码400的默认 http错误文本.

"Bad Request" is the default http error text for the status code 400.

因此这里有此处记录的设置此处概述

<configuration>
  <system.webServer>
    <httpErrors existingResponse="PassThrough"></httpErrors>
  </system.webServer>
</configuration>

请仔细咨询您所用IIS版本的文档,其中存在许多细微的差异.

Consult the documentation carefully for your version of IIS, there is lots of subtle version differences.

编辑

并不是专门针对MVC,但是这是我曾经解决它的方式(生产代码的一部分),而且似乎也对OP有所帮助:

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:

Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
Response.ContentType = "text/plain";
Response.Write(new String('_', 513) + "my custom message");

根据IIS,可能需要或可能不需要最小的字符限制限制版本.如果有人能进一步说明这种证据不足的行为,我将不胜感激.

This absurd minimum character limit thing may or may not be needed depending on IIS version. I would too be grateful if somebody could shed a little more light on this underdocumented behavior.

这篇关于ASP.NET MVC 5 Ajax错误statusText始终为“错误".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆