1

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?

redsquare
  • 78,161
  • 20
  • 151
  • 159
Mickey Shine
  • 12,187
  • 25
  • 96
  • 148

2 Answers2

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 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
  • 27,240
  • 15
  • 95
  • 114
  • 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 Aug 08 '09 at 10:31
  • this is jsonp so not an xhr request. it uses script tags rather than xmlhttp so you cant set synch or asych – redsquare Aug 08 '09 at 10:42
  • @Mickey Shine check update. @redsquare you are right, I never used it so far, the documentation is good though – Elzo Valugi Aug 08 '09 at 10:53
  • also getjson is just a simple wrapper for .ajax – redsquare Aug 08 '09 at 10:57
  • Thanks for all your responses. But what can I do with this? Is there any way that I can solve this problem? – Mickey Shine Aug 10 '09 at 02:58
  • as redsquare suggested try to put an example online with some more code. will take a look – Elzo Valugi Aug 10 '09 at 11:14
  • also check this post http://stackoverflow.com/questions/1002367/jquery-ajax-jsonp-ignores-a-timeout-and-doesnt-fire-the-error-event – Elzo Valugi Aug 10 '09 at 11:53
0

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
  • 78,161
  • 20
  • 151
  • 159
  • What you mean about "overriding the callback part"? More specifically please? – Mickey Shine Aug 08 '09 at 10:08
  • http://some.com:8000/cometd?callback=jsonp1249728075313&_=1249728216558 http://some.com/a.php?callback=jsonp1249728075314&_=1249728219580 here are the URLS, I think they are not the same (jsonp12497...). Can you help me with this? – Mickey Shine Aug 08 '09 at 10:25
  • What do you mean by long polling request. Does the first jsonp call just take a long time to return? – redsquare Aug 08 '09 at 10:43
  • 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 Aug 08 '09 at 11:31
  • @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 Aug 10 '09 at 02:57
  • 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 Aug 10 '09 at 08:00