admin 管理员组文章数量: 1086019
I am using $.ajax to make a post request. Then, I am trying to call a function before success or failure of the ajax request. 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 pletes irrespective of whether it succeeds or fails but before success or error callback is called. I dont want to use ajaxStart and ajaxStop in this scenario. I looked at 'plete' callback, but it is only called after error or success. I don't want to call the someCallBack() at two places. Any Suggestions, Please.
I am using $.ajax to make a post request. Then, I am trying to call a function before success or failure of the ajax request. 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 pletes irrespective of whether it succeeds or fails but before success or error callback is called. I dont want to use ajaxStart and ajaxStop in this scenario. I looked at 'plete' callback, but it is only called after error or success. I don't want to call the someCallBack() at two places. Any Suggestions, Please.
Share Improve this question asked Nov 14, 2014 at 0:19 sdhakalsdhakal 371 silver badge4 bronze badges 2- Why do you need it to be called before the success or error callbacks? What functional dependency is there? – Bergi Commented Nov 14, 2014 at 0:22
-
Use promises syntax - then you can use
.always()
, followed by.done()
and.fail()
– zerkms Commented Nov 14, 2014 at 0:23
4 Answers
Reset to default 4I think you're looking for ajax' deferred interface, which has an always
method. And you can chain your success/error handlers after that - though you cannot use those that go into the options object.
$.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 supports beforeSend
$.ajax({
url: "http://example.",
beforeSend: function( xhr ) {
xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
}
}).always(function() {
callback();
});
I misread your question. I sounds like you need .always()
?
$.ajax returns a $.Deferred (more specifically, a jqXHR), which you can use to define your callbacks. 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:
($.Deferred()
behaves just like your $.ajax(settings)
call)
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:
$.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
.
For example
$(document).bind("ajaxSend", function(){
$("#loading").show();
}).bind("ajaxComplete", function(){
$("#loading").hide();
});
本文标签: javascriptCall a function before ajax Success or FailureStack Overflow
版权声明:本文标题:javascript - Call a function before ajax Success or Failure - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744025085a2520490.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论