Synchronous JQuery.post ()

I am writing a small script that makes individual AJAX calls through a loop, and I ran into the most likely obvious problem. It seems that the loop is going to quickly process the data received with ajax, as a result of which it only loads the last piece of data in the loop. I added a notification window that goes through iterations and loads data fine, but it will not be practical in a user environment. The code is just jquery.post () with a callback inside the for loop. I can publish the code on demand, but I feel it can be clarified verbally. Does anyone know a workaround or a better approach to loading data sequentially?

EDIT

.ajaxSetup() is .ajaxSetup() .post() ? Perhaps I can use this to change the asynchronous value for .post () ..

+8
javascript jquery synchronization ajax
source share
4 answers

I really found that adding this snippet worked, so I didn't have to change my .post () to .ajax ()

$.ajaxSetup({ async: false });

I'm not sure it will change the settings of my other ajax calls either, though

+4
source share

You need to make your ajax call be synchronous to my friend;)

http://api.jquery.com/jQuery.ajax/

Example:

asyncBoolean Default: true

By default, all requests are sent asynchronously (i.e., by default, this value is true) . If you need synchronous requests, set this parameter to false. Cross-domain requests and dataType: jsonp requests do not support synchronous operation. Please note that synchronous requests can temporarily block the browser, disabling any actions while the request is active.

+5
source share

If you use async: false , you can queue each of your ajax calls until they are executed in a synchronous way. For example:

  for( var i=0;i < x;i++ ){ $.ajax({url: 'myurl', async: false, success: function(data){ //do something with the returned 'data' }; }); } 
0
source share
 function myFunction() { var x; for(var i=0;i<10;i++){ if (confirm("Press a button!") == true) { x = "You pressed OK!"; } else { x = "You pressed Cancel!"; } document.getElementById("demo").innerHTML = x; } } enter code here 
0
source share

All Articles