views:

1247

answers:

2

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?

A: 

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.

redsquare
What you mean about "overriding the callback part"? More specifically please?
Mickey Shine
Mickey Shine
What do you mean by long polling request. Does the first jsonp call just take a long time to return?
redsquare
Are you sure you server side script wraps the json in the correct callback name. maybe your mixing it up server side and calling the wrong function.
redsquare
@redsquare, "long polling" can be understood as a connection takes long time to return. It's just a term when doing comet chat. And I am sure the response of the server is correct, and also if I change "JSONP" to "POST"(real XHR request), the "onSuccess" event can be executed on time.
Mickey Shine
Yes your trying a comet implementation. Does your first request actually return anything before the second request? I have prototyped this locally and my first request basically sleeps for 20 secs whilst a call a second request returns in the meantime. Can you put your page online and I can debug.
redsquare
A: 

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
}
Elzo Valugi
Thanks for your answer. Now they are executed asynchronous, only the "onSuccess" event of one connection is blocked. And it behaves like they are synchronous (and I don't want it to be so).I am sure the ajax connection is executed and get the content immediately (I can see this through httpfox), but the only problem is the "onSuccess" event, the event is postponed.
Mickey Shine
this is jsonp so not an xhr request. it uses script tags rather than xmlhttp so you cant set synch or asych
redsquare
@Mickey Shine check update.@redsquare you are right, I never used it so far, the documentation is good though
Elzo Valugi
also getjson is just a simple wrapper for .ajax
redsquare
Thanks for all your responses. But what can I do with this? Is there any way that I can solve this problem?
Mickey Shine
as redsquare suggested try to put an example online with some more code. will take a look
Elzo Valugi
also check this post http://stackoverflow.com/questions/1002367/jquery-ajax-jsonp-ignores-a-timeout-and-doesnt-fire-the-error-event
Elzo Valugi