Jquery HttpStatusCodeResult.StatusDescription的可靠性如何?

Jquery HttpStatusCodeResult.StatusDescription的可靠性如何?,jquery,ios,angularjs,asp.net-mvc,asp.net-mvc-5,Jquery,Ios,Angularjs,Asp.net Mvc,Asp.net Mvc 5,考虑以下控制器操作,其中有大量的例子在web上: public ActionResult Fail() { return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "My helpful message"); } 现在,我们将通过Ajax调用该操作,并在页面中显示结果 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <

考虑以下控制器操作,其中有大量的例子在web上:

public ActionResult Fail()
{
    return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "My helpful message");
}
现在,我们将通过Ajax调用该操作,并在页面中显示结果

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script type="text/javascript">
            var xmlhttp = new XMLHttpRequest();

            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
                    document.body.innerHTML = xmlhttp.response;
                }
            }

            xmlhttp.open("POST", "/log/fail", true);
            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlhttp.send();

        </script>
    </head>
    <body>
    </body>
</html>
jQuery

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
        <script type="text/javascript">
            $.ajax({ url: '/log/fail', method:'POST' }).error(function (response) {
                document.body.innerHTML = response.statusText; // 'Bad request' in iOS
            });
        </script>
    </head>
    <body>
    </body>
</html>

$.ajax({url:'/log/fail',方法:'POST'})。错误(函数(响应){
document.body.innerHTML=response.statusText;///iOS中的“错误请求”
});

您可以依赖服务器返回的状态代码结果(至少在IIS上)

但是,不能保证浏览器将支持它

但是您的断言是正确的,即您应该使用模型返回任何错误以显示在客户端上,以获得一致的结果

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
        <script type="text/javascript">
            $.ajax({ url: '/log/fail', method:'POST' }).error(function (response) {
                document.body.innerHTML = response.statusText; // 'Bad request' in iOS
            });
        </script>
    </head>
    <body>
    </body>
</html>