问题
Right now I use:
$("#status")
.on("ajaxSend", function() { $(this).show(); } )
.on("ajaxComplete", function() { $(this).hide(); } );
To get a status when ajax was working. This worked with json, but not with jsonp. Is there a way to get this to work with jsonp? Thank you.
回答1:
This appears to be a bug in jQuery, or at least consideration for a bug.
Unfortunately you can't do anything about it at this point except update your jsonp requests to use the same things on beforeSend
and complete
:
function ajaxSend() { $("#status").show(); }
function ajaxComplete() { $("#status").hide(); }
$.ajaxSend(ajaxSend);
$.ajax({
dataType: 'jsonp',
beforeSend: ajaxSend,
complete: ajaxComplete
});
回答2:
Being a relative newbie to jQuery & ajax I'm not sure if this answers your question, but I've found that event handlers inside ajaxSetup()
will automatically be called for jsonp requests, whereas standalone handlers such as $(document).ajaxSend(function(event, jqXHR, settings){});
will not. And as Explosion Pills said, you can also manually override these on a per-call basis.
$.ajaxSetup({
beforeSend: function(jqXHR, settings) {
},
complete: function(jqXHR, textStatus) {
}
});
来源:https://stackoverflow.com/questions/14573522/equivalent-to-ajaxsend-and-or-ajaxcomplete-with-jsonp