Note that there are some explanatory texts on larger screens.

plurals
  1. PO$.ajax() callbacks are not bound to their specific request?
    text
    copied!<p>The code is very complex so i have simplified below in order to confirm if the behavior i am experiencing is normal or due so some other error i have made in the code.</p> <p>I have two separate ajax requests that each have their own unique call back. I do not care which one completes first and one has no dependency on the other</p> <pre><code>function ajax(url, cbS){ $.ajax({ url: url, contentType: 'application/json', dataType: 'json', success: function(data){ cbS(data) }, }); } function callbackSuccess1(data){ $('#div1').html(data) } function callbackSuccess2(data){ $('#div2').html(data) } //request#1 ajax(myapiurl+'&amp;peram1=100', callbackSuccess1); //request#2 ajax(myapiurl+'&amp;peram2=200', callbackSuccess2); </code></pre> <p>The problem: Sometimes callbackSuccess1 gets the data intended for request#2 and vice versa. </p> <p>It seems that which ever request completes first fires callbackSuccess1 and the second to complete fires callbackSuccess2. </p> <p>I need the callback to be bound to it's specific request so that regardless of the order in which they complete each request fires it's proper callback.</p> <p>OTHER INFO: My backed is django-tastypie, at this point i am thinking that tastypie is somehow messing up the response. That is the only logical conclusion, given that the javascript seems to be immutable.</p> <p>The proof that this is actually occurring is that when i inspect the responce on request#1 the data objects are clearly intended for request#2...</p> <p>CONCLUSION: Thanks for confirming that 'each invocation of your ajax() function will create it's own closure'. This was what i thought was going wrong. I found the problem in my API. I was doing some funky stuff and it looks like I had a variable that was not getting trashed in time causing the API to return the wrong data if the first request took longer than the second.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload