Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I stop $.post in jQuery working asynchronously?

Tags:

jquery

ajax

I have the following calls:

getData(0);
getData(1);
getData(2);

These call a javascript function:

function getData(Code) {
  $.post('/adminA/GetData',
                { Code: Code },
  function (data) {

The problem is that my data gets returned out of order. Is there a way that I can have the data return in order?

like image 487
Jennifer82 Avatar asked Jun 13 '11 03:06

Jennifer82


2 Answers

Another alternative is to use ajaxSetup

function getData(Code) {
  $.ajaxSetup({async: false});
  $.post('/adminA/GetData',{ 
        Code: Code },  function (data) {
         // Do Something
        }); 
   $.ajaxSetup({async: true}); //So as to avoid any other ajax calls made sybchrounously
}
like image 90
Chandu Avatar answered Nov 15 '22 10:11

Chandu


You can use $.ajax instead and set async to false.

like image 21
Bemmu Avatar answered Nov 15 '22 11:11

Bemmu