问题读取HTTP状态/错误$ C $从jQuery的AJAXÇ [英] Problems Reading the HTTP Status/Error Code from jQuery AJAX

查看:159
本文介绍了问题读取HTTP状态/错误$ C $从jQuery的AJAXÇ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图妥善处理一个HTTP状态错误(404,501等)可能在任何jQuery的AJAX调用(我使用的是1.6.4版本),但是,对于我的生活,我可以'被退回吨得到了相应的反应code(所有的值是'0'或'错误',没有什么更具体的)。

I'm trying to appropriately handle a HTTP status error (404, 501, etc) that might be returned by any AJAX call in jQuery (I'm using version 1.6.4) however for the life of me I can't get out the appropriate response code (all values are '0' or 'error' and nothing more specific).

更新:创建的jsfiddle 这里

UPDATE: created a JSFIDDLE here

更新:加入状态code:{***} 按3nigma的建议,但它不火

UPDATE: added statusCode: { *** } as per 3nigma's suggestion BUT which does not fire

<!DOCTYPE html><html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    <script type="text/javascript">
        function doAjax()
        {
            $.ajax({
                type: "GET",
                url: "http://www.non-existant-url.com/",
                success: function(data, textStatus, jqXHR){
                    alert("Success");
                },
                error: function (xhr, textStatus, errorThrown) {
                     console.log("xhr.status: " + xhr.status);
                     console.log("xhr.statusText: " + xhr.statusText);
                     console.log("xhr.readyState: " + xhr.readyState);
                     console.log("xhr.responseText: " + xhr.responseText);
                     console.log("xhr.responseXML: " + xhr.responseXML);
                     console.log("textStatus: " + textStatus);
                     console.log("errorThrown: " + errorThrown);
                     console.log("xhr.redirect: " + xhr.redirect);
                        },
                statusCode: {
                    404: function () { console.log("404"); },
                    501: function () { console.log("501"); },
                    502: function () { console.log("502"); }
                }
            });
        }
    </script>
</head>
<body><button onclick="doAjax();">Do AJAX</button></body>
</html>

我得到的输出如下:

The output I get is as follows:

仅供参考,我在......学习文档

FYI I've studied the documentation at...

http://api.jquery.com/jQuery.ajax/ (以下简称错误(jqXHR,textStatus,errorThrown)一节) http://api.jquery.com/jQuery.ajax/#jqXHR

http://api.jquery.com/jQuery.ajax/ (the "error(jqXHR, textStatus, errorThrown)" section) http://api.jquery.com/jQuery.ajax/#jqXHR

但jqXHR似乎并没有被填充正确。我必须做一些错误的,现在我已经江郎才尽了,所以我真的很AP preciate一些帮助。谢谢!

but the jqXHR doesn't seem to be populating properly. I must be doing something wrong and I've run out of ideas now so I would really appreciate some help. Thanks!

推荐答案

我是numpty,阿贾克斯并不出于安全考虑,工作跨域。如果我更改URL,以便其目标定位在同一个域中的主机的话,状态codeS和文字等被奇迹般地填充。

I'm a numpty, ajax doesn't work cross-domain for security reasons. If I change the url so that its targetting the same domain as the host then the status codes and text etc is miraculously populated.

我添加了另一个的jsfiddle的例子来证明这一点 rel="nofollow">。

I've added another JSFIDDLE example to demonstrate this here.

这篇关于问题读取HTTP状态/错误$ C $从jQuery的AJAXÇ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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