1

I am trying to make a jsonp call to server and here is how code looks like:

function myCallback(data) {
  var self = this;
  var currentId = data.ID;
  if(currentId == self.latestId){
    console.log("latest request responded, do something");
    self.latestId = new Date().getTime(); 
  } else {
    console.log("old request responded, so we don't care");
  }
};

function makeCall() {
  var self = this;
  self.latestId = new Date().getTime();
  var url = serverUrl + "&ID="+ self.latestId;
  $.ajax(url,{
   dataType: "jsonp",
   timeout: 10000,
   cache: true,
   jsonp:"callback",
   jsonpCallback: "myCallback",
   error: function(id) {
     return function() {
         if(id == self.latestId) {
           console.log("latest request errored out");//might be due to timeout
           self.latestId = new Date().getTime(); // updating latestid so mycallback ignore this request
         }
         else {
           console.log("old request errored out, so we don't care anymore");
         }
     }
   }(self.latestId)
  });
};

response from server look something like myCallback({some_data...});

When there is only one request get fired it works as expected, but when I fire 4-5 request with same data other then Id control neither go to myCallback nor to error, while server responded successfully in time. This doesn't happen consistently, so looks like a synchronization issue. Can there be any synchronization issue in using named callback while making multiple queries. In case of multiple queries only last one should win, but doesn't look like to be working here. Can it be a problem due to same callback name for every request?

I am kind of clueless about what is happening here, does someone has faced something like this.

EDIT: Looks like it is something to do with callback name for parallel requests which is mentioned Parallel JSONP requests in jQuery do not trigger multiple "callback events"?

Community
  • 1
  • 1
GG.
  • 2,835
  • 5
  • 27
  • 34
  • Is this a typo on row 4? `if(currentId == self.larestId){` – Niklas Oct 16 '12 at 07:38
  • yeah that was a typo here, but this is just to give an idea what I am trying to do. Still have the issue :( – GG. Oct 16 '12 at 07:45
  • By specifying the jsonpCallback function and specifying cache:true you have made caching easy for the browser... do I understand correctly that you now do not want this caching anymore ? – AardVark71 Oct 16 '12 at 08:07
  • I think I can nuke off caching from here, because everytime url will be different(since I am appending time in millisec as ID). – GG. Oct 16 '12 at 08:10
  • Btw, do you have the issue in all browsers? – AardVark71 Oct 16 '12 at 08:20
  • I have it in Firefox, let me check with chrome also – GG. Oct 16 '12 at 08:21
  • I see this issue in both firefox and chrome. – GG. Oct 16 '12 at 08:50
  • Looks like the post you found explains it pretty well. Without specifying the jsonpCallback you'll generate a random function name for each JSONP call to avoid caching. – AardVark71 Oct 16 '12 at 09:22

0 Answers0