什么是Ajax调用:“成功”或“失败”? [英] What is an Ajax call: 'success' or 'failure'?

查看:147
本文介绍了什么是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 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.

这篇关于什么是Ajax调用:“成功”或“失败”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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