jsonp 没有在发送之前触发?

发布于 2024-08-31 02:40:26 字数 804 浏览 1 评论 0原文

我正在开发一个项目,使用 $.ajax 并将 dataType 设置为 jsonp 从不同域调用 Web 服务。

    $.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”没有被解雇。

当应用程序位于同一域(使用 jsonp)时,动画工作正常,但是当我将应用程序移动到不同的服务器时,除了“beforeSend”没有被调用之外,一切正常。所以用户将无法看到加载动画。

有什么解决方法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若能看破又如何 2024-09-07 02:40:26

跨域JSONP请求不使用XMLHTTPRequest,因此事件流程不同。

您可以在调用 $.ajax 后立即显示加载动画。

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.

挽袖吟 2024-09-07 02:40:26

jQuery JSONP 的 beforeSend 实现仍在进行中。
目前的解决方案是,

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

$.ajax({...}); 之前使用:

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({...});.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文