最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery ajax onSuccess event - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Aug 8, 2009 at 14:42 redsquare 78.7k21 gold badges154 silver badges159 bronze badges asked Aug 8, 2009 at 9:41 Mickey ShineMickey Shine 12.6k26 gold badges97 silver badges151 bronze badges 1
  • Amended my anwser . – redsquare Commented Aug 8, 2009 at 10:11
Add a ment  | 

2 Answers 2

Reset to default 2

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 plete() 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
}

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.

发布评论

评论列表(0)

  1. 暂无评论