什么是Ajax调用:“成功”或“失败”?Ajax

由网友(你忘了带我走)分享简介:这似乎是101级,但是我无法找到答案! (而不是我可以链接到该怎么做就成功或失败,jQuery的阿贾克斯()。成功等。This seems 101-level, but I can't find an answer! (instead I find links to what to do on success or...

这似乎是101级,但是我无法找到答案! (而不是我可以链接到该怎么做就成功或失败,jQuery的阿贾克斯()。成功等。

This seems 101-level, but I can't find an answer! (instead I find links to what to do on success or failures, jQuery's ajax().success and the like.

我的直觉是:

成功:200 HTTP返回code 失败:别的。 success: 200 HTTP return code failure: anything else.

推荐答案

这是不是一个真正的AJAX,但jQuery的问题,因为成功的回调jQuery的,而不是AJAX对整个唯一特征。

This is not really an AJAX, but jQuery question, because the success callback is only characteristic of jQuery, not AJAX on the whole.

从jQuery源代码,你可以看到它是如何实现的。

From the jQuery source you can see how it is implemented

// If successful, handle type chaining
            if ( status >= 200 && status < 300 || status === 304 ) {

                // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
                if ( s.ifModified ) {

                    if ( ( lastModified = jqXHR.getResponseHeader( "Last-Modified" ) ) ) {
                        jQuery.lastModified[ ifModifiedKey ] = lastModified;
                    }
                    if ( ( etag = jqXHR.getResponseHeader( "Etag" ) ) ) {
                        jQuery.etag[ ifModifiedKey ] = etag;
                    }
                }

                // If not modified
                if ( status === 304 ) {

                    statusText = "notmodified";
                    isSuccess = true;

...
else fail with the error callback

因此​​,基本上,如果HTTP响应code是200至299(含)或304不言而喻了成功回调,否则,它是错误回调。

So, basically if the HTTP response code is between 200 and 299 (inclusive) or 304 it goes for the success callback, otherwise, it is the error callback.

阅读全文

相关推荐

最新文章