Jquery ajax onSuccess 事件

发布于 2024-07-30 01:32:09 字数 834 浏览 2 评论 0原文

我正在使用 JQuery 执行 AJAX,但每次“onSuccess”事件都必须在另一个 AJAX 请求断开连接后执行。

代码如下:

    d.ajax({
        url: f.options.url.offline,
        dataType: "jsonp",
        jsonp: "callback",
        cache: false,
        data: {
            status: "offline",
            ticket: f.connection.options.ticket
        },
        success: function(g) {
            f._offlineSuccess()
        },
        error: function() {
            f._offlineError()
        }
    })

我所有的 AJAX 请求都是 JSONP,当触发上述代码时,同时已经建立了另一个 AJAX 连接(长轮询请求,持续约 10 秒)。 因此“f._offlineSuccess”函数总是在另一个AJAX连接断开后执行。

我看不到两个 AJAX 请求之间有任何关系,而且我不知道为什么必须在另一个 AJAX 连接停止后执行“onSuccess”函数。

感谢任何帮助〜

==================================

更新:

我刚刚发现我是否有两个JSONP连接同时,“onSuccess/onFailure”函数将被阻塞。 不知道以前有人遇到过同样的问题吗?

I am doing AJAX with JQuery but every time the "onSuccess" event must be executed after another AJAX request disconnected.

Here is the code:

    d.ajax({
        url: f.options.url.offline,
        dataType: "jsonp",
        jsonp: "callback",
        cache: false,
        data: {
            status: "offline",
            ticket: f.connection.options.ticket
        },
        success: function(g) {
            f._offlineSuccess()
        },
        error: function() {
            f._offlineError()
        }
    })

All my AJAX requests are JSONP, and when the above code is triggered, there is another AJAX connection (long polling request, last about 10 senconds) already established in the mean time. So the "f._offlineSuccess" function is always executed after another AJAX connection disconnected.

I can not see any relationship between the two AJAX requests, and I don't know why the "onSuccess" function must be executed after another AJAX connection stopped.

Any help is appreciated~

================================

updated:

I just found out if I have two JSONP connection at the same time, the "onSuccess/onFailure" function will be blocked. I don't know if some one encountered the same problem before?

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

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

发布评论

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

评论(2

栩栩如生 2024-08-06 01:32:09

Ajax 请求是异步的。 因此,新的请求不会导致前一个请求完成。 如果您希望该行为,请使用 async 参数为 false,或使用complete() 函数来调用另一个请求。 仅当第一个请求完成时才会触发。

更新
对于 JsonP 使用 jQuery.getJSON() 并在回调上执行第二个请求(如果调用)很成功。

function (data, textStatus) {
    // data will be a jsonObj
    // textStatus will be one of the following values: 
    //   "timeout","error","notmodified","success","parsererror"
    this; // the options for this ajax request
}

Ajax requests are asynchronous. so a new request is not going for the previous one to finish. If you want that behaviour use async parameter to false, or use the complete() function to call for another request. This will fire only when the first request is finished.

UPDATE
For JsonP use jQuery.getJSON() and do the second request on callback if the call was succesfull.

function (data, textStatus) {
    // data will be a jsonObj
    // textStatus will be one of the following values: 
    //   "timeout","error","notmodified","success","parsererror"
    this; // the options for this ajax request
}
尘曦 2024-08-06 01:32:09

如果您使用 firebug - net 选项卡,您将能够看到两个 jsonp 请求的完整 url。 您应该能够在 url 末尾看到回调函数名称。 这些是不同的还是相同的? 我只能假设它们是相同的。

If you use firebug - net tab, you will be able to see the full url of the two jsonp requests. You should be able to see the callback function names on the end of the url. Are these different or the same? I can only assume they are the same.

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