AJAX JQuery Deferred callbacks only get called once
I am registering callbacks on a few jquery ajax objects.
So I have:
function main_getLogin( options ){
g_userXHR = $.ajax({
url:'ajx/_account.aspx',
type:'POST' ,cache:false, dataType:'json'
})
.success( function(data){
//var loginData = data.ACCOUNT_INFO;
//dlog(data);
});
}
And a call back later - elsewhere
g_userXHR.success(function(d,status){
d = d.ACCOUNT_INFO;
if(d.NAME != "" && d.ERRORS.length == 0){//logged in
$('#myRHpage .registeredUser').show();
$('#myRHpage .unregisteredUser').hide();
setupTabs();
}
else{//user开发者_运维技巧 is not logged in
$('#myRHpage .unregisteredUser').show();
$('#myRHpage .registeredUser').hide();
}
});
This callback and all the others work perfectly the first time I am kicking off
main_getLogin();
the .success connected directly to g_userXHR gets called every-time, however the other callbacks attached outside of the function only gets fired the first time I call main_getLogin();
In addition, is there any way to declare a ajax object and send it later. like g_userXHR.send();
Thank you
If that code that's "elsewhere" does not re-add those "success" callbacks every time "main_getLogin()" is called, then they won't be run. Each call to "$.ajax" constructs a brand new "Deferred" object, so what you did to the first one won't affect the next such object jQuery builds for you.
If you want to always do certain things upon success, you can use "$.ajaxSuccess()":
$.ajaxSuccess(function() { ... });
精彩评论