jsonp is not firing beforeSend?

馋奶兔 提交于 2020-01-12 08:52:58

问题


I am working on a project to call a webservice from different domain using $.ajax with dataType set to jsonp.

    $.ajax({
        type: "GET",
        url: testService.asmx,
        async: true,
        contentType: "application/json; charset=utf-8",
        dataType: "jsonp",

        beforeSend: function (XMLHttpRequest) {
            alert('Before Send'); //Nothing happnes
        },
        success: function (response) {
            alert('Success'); //this was fired
        },
        complete: function (XMLHttpRequest, textStatus) {
            alert('After Send'); //this was fired
        }
    });

The problem is I have a ...loading animation which I want to display while the web service request is being processed. I tried using "beforeSend:" to show the loading animation but it seems like "beforeSend" is not getting fired.

The animation works fine when the app is on the same domain (using jsonp) but when I move the app into a different server, everything works except "beforeSend" is not getting called. So users will not be able to see the loading animation.

Is there any workaround for this?


回答1:


Cross-domain JSONP requests do not use XMLHTTPRequest, so the event flow is different.

You can simply show your loading animation right after calling $.ajax.




回答2:


beforeSend implemention for jQuery JSONP is sill in progress. The solution for now, use:

jQuery(document).ajaxStart(function(){alert('Before Send');}); // For all XHRs
$('#id').ajaxStart(function(){alert('Before Send');});         // For unique XHR

before $.ajax({...});.



来源:https://stackoverflow.com/questions/2774716/jsonp-is-not-firing-beforesend

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