简体   繁体   English

在ajax成功或失败之前调用一个函数

[英]Call a function before ajax Success or Failure

I am using $.ajax to make a post request. 我正在使用$ .ajax进行发布请求。 Then, I am trying to call a function before success or failure of the ajax request. 然后,我尝试在ajax请求成功或失败之前调用一个函数。 Right now my approach is like following 现在我的方法就像

var someCallback = function() {
     //do something
};
var Success = false;
$.ajax({
    type: "POST",
    url: "/some/service",
    dataType: "text",
    data: JSON.stringify(someData),
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        someCallBack(); //<--- this is the function
        Success = true;//doesnt goes here
    },
    error: function (textStatus, errorThrown) {
        someCallBack();
        Success = false;//doesnt goes here
    }

});

The thing is, i need to run someCallBack() after the ajax request completes irrespective of whether it succeeds or fails but before success or error callback is called. 事情是,我需要在ajax请求完成后运行someCallBack(),而不管它是成功还是失败,但是在调用成功或错误回调之前。 I dont want to use ajaxStart and ajaxStop in this scenario. 我不想在这种情况下使用ajaxStart和ajaxStop。 I looked at 'complete' callback, but it is only called after error or success. 我查看了“完成”回调,但仅在错误或成功后才调用。 I don't want to call the someCallBack() at two places. 我不想在两个地方调用someCallBack()。 Any Suggestions, Please. 任何建议,请。

AJAX supports beforeSend AJAX支持beforeSend

$.ajax({
  url: "http://example.com",
  beforeSend: function( xhr ) {
    xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
  }
}).always(function() {
   callback();
});

I misread your question. 我看错了你的问题。 I sounds like you need .always() ? 我听起来像您需要.always()吗?

I think you're looking for ajax' deferred interface, which has an always method . 我认为您正在寻找ajax的 延迟接口,该接口具有always方法 And you can chain your success/error handlers after that - though you cannot use those that go into the options object. 之后,您可以链接成功/错误处理程序-尽管您不能使用options对象中的成功/错误处理程序。

$.ajax({
    type: "POST",
    url: "/some/service",
    dataType: "text",
    data: JSON.stringify(someData),
    contentType: "application/json; charset=utf-8"
})
.always(someCallBack)
.done(function (data) {
    var Success = true; // goes here after someCallBack()
})
.fail(function (textStatus, errorThrown) {
    var Success = false; // goes here after someCallBack()
});

$.ajax returns a $.Deferred (more specifically, a jqXHR ), which you can use to define your callbacks. $ .ajax返回$ .Deferred(更具体地说,是jqXHR ),您可以使用它来定义回调。 In your case, the advantage to this over defining callbacks within the ajax setting object is that your callbacks will be executed in the order that they are defined: 在您的情况下,与在ajax设置对象中定义回调相比,这样做的好处是,您的回调将按照定义的顺序执行:

( $.Deferred() behaves just like your $.ajax(settings) call) $.Deferred()行为就像您的$.ajax(settings)调用一样)

Success: 成功:

$.Deferred()
  .always(function() { console.log('always'); })
  .done(function() { console.log('done'); })
  .fail(function() { console.log('fail'); })
  .resolve();
// "always"
// "done"

Failure: 失败:

$.Deferred()
  .always(function() { console.log('always'); })
  .done(function() { console.log('done'); })
  .fail(function() { console.log('fail'); })
  .reject();
// "always"
// "fail"

Likewise, if .always is defined last, it will execute last: 同样,如果.always最后定义,它将最后执行:

$.Deferred()
  .done(function() { console.log('done'); })
  .fail(function() { console.log('fail'); })
  .always(function() { console.log('always'); })
  .resolve();
// "done"
// "always"

You just need to bind 2 ajax events: ajaxSend and ajaxComplete . 您只需要绑定2个ajax事件: ajaxSendajaxComplete For example 例如

$(document).bind("ajaxSend", function(){
    $("#loading").show();
}).bind("ajaxComplete", function(){
    $("#loading").hide();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM