Gaetan Gaetan - 5 months ago 30
jQuery Question

jQuery Ajax request async false

I'd like to know why I get this error if I send a ajax request like:

$.ajax({
url: "testOperation.php",
async: false,
data: compareHeure,
success: function (data, statusRequest) {
})


error:


Synchronous XMLHttpRequest on the main thread is deprecated because of
its detrimental effects to the end user's experience. For more help,
check https://xhr.spec.whatwg.org
/.


Is there a problem ?

Thanks,

Jai Jai
Answer

I'd like to know why I get this error

The short answer is because of this option in the ajax:

async: false,

There could be other better solution to the problem you currently have. If you post more details about why do you need to use Synchronous ajax?


Either use this way:

$.ajax({
   url:'',
   type:'post',
   success:function(data){
       // make another ajax call here then    
   }
})

You can use jQuery.when():

$.when( $.ajax("/page1.php"), $.ajax("/page2.php") ).done(function(a1, a2) {
    // here you can get both ajax response when both gets completed.
    console.log(a1);
    console.log(a2);
});

Here .done() only executed when both ajax gets completed.

Comments