jQuery的Ajax事件调用顺序 [英] jQuery ajax event invoking order

查看:76
本文介绍了jQuery的Ajax事件调用顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让说我有这样一个简单的功能。

Let say I have a simple function like this.

$('body').ajaxSuccess(function(){alert('global');}
                      );


$.post('http://www.google.com', 
      { name: "John", time: "2pm" } ,
      function(data,s,xhr) {
  alert('local');

});

http://jsfiddle.net/AT5vt/

有可能使局部成功回调之前得到调用了全局的ajaxSuccess()函数?正如我希望通过当地的功能做的结果是一个全球性的检查之前,进一步的处理。

It is possible to make the global ajaxSuccess() function getting invoked before the local success callback? As I want to do a global checking on the result before further processing by the local functions.

推荐答案

使用自定义后处理程序使用默认AJAX后代替:

use the default ajax post instead of using the custom post handler:

http://jsfiddle.net/AT5vt/1/

$('body').ajaxSuccess(function() {
     alert('global success');
  }
);

$('body').ajaxError(function() {
     alert('global error');
  }
);

$.ajax({
  type: 'POST',
  url: 'http://www.google.com',
  data: { name: "John", time: "2pm" } ,
  complete: function(data,s,xhr) {
    if(xhr.status == 200) {
      alert('local success');
    } else {
      //note you can catch a lot of errors here like 404, 500, 406, 302 etc
      alert("local error");
    }
  }
})

并没有把它的jsfiddle为张贴到google.com从那里没有保留,但应该工作。

didn't put it in jsfiddle as posting to google.com keeps on failing from there but that should work.

这篇关于jQuery的Ajax事件调用顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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