Question

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?

Was it helpful?

Solution

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
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top