JSONP不点火beforeSend?JSONP、beforeSend

2023-09-10 13:21:58 作者:花落人憔悴

我工作的一个项目叫使用$阿贾克斯由于数据类型设置为JSONP不同域中的Web服务。

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
        }
    });

现在的问题是我有一个...加载动画,我想时显示的Web服务请求正在处理中。我试着使用beforeSend:显示加载动画,但它似乎像beforeSend是不是被解雇

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.

动画正常工作时,该应用程序是在同一个域(使用JSONP),但是当我移动应用程序到不同的服务器上,一切工作除了beforeSend是没有得到调用。因此,用户将不能够看到加载动画

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.

有什么解决方法吗?

推荐答案

跨域JSONP请求不使用XMLHTT prequest,因此事件流是不同的。

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.