jQuery的Ajax事件调用顺序顺序、事件、jQuery、Ajax

由网友(王权在手)分享简介:让说我有这样一个简单的功能。Let say I have a simple function like this.$('body').ajaxSuccess(function(){alert('global');});$.post('http://www.google.com', { name: "John", t...

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

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.

阅读全文

相关推荐

最新文章