Equivalent to ajaxSend and/or ajaxComplete with jsonp?

喜夏-厌秋 提交于 2019-12-24 01:56:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!