确定AJAX调用成功的正确方法是什么?

时间:2009-05-29 10:57:49

标签: javascript ajax httpresponse

确定AJAX调用是否成功的正确方法是什么?

我在prototype.js中看到

    return !status || (status >= 200 && status < 300);

并在jQuery中:

    // IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450
    return !xhr.status && location.protocol == "file:" ||
        ( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status == 1223;

哪一个是正确的?如果我们不使用任何Javascript库但是在基本Javascript中编写AJAX,我们应该使用哪一个?

2 个答案:

答案 0 :(得分:5)

原型似乎更“正确”,因为它只将有效的HTTP成功代码视为成功。 jQuery更加健壮,因为它考虑了错误和其他经常成功的代码。

答案 1 :(得分:1)

HTTP状态属于以下类别:

2XX:成功

3XX:重定向

4XX:客户端错误

5XX:服务器错误。

因此,200-300是ajax调用的“ok”结果。

有关状态代码的详细信息,请查看http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

欢呼声,

JRH。

相关问题