Implement ajax synchronization request

The four methods of calling ajax in jQuery: $ .get (), $ .post (), $ getJSON () and $ ajax () are all asynchronous requests. But sometimes you have to use it and you need to synchronize requests. Then we can refer to the following methods

First, $ .get (), $ .post () and $ getJSON () can be set directly before performing the operation.

 //$.post()    
 $.ajaxSettings.async = false;    //执行之前,设置为同步
 $.post("url", data, function(result) {    });
 $.ajaxSettings.async = true;     //可选:执行完后,设置回异步

//$.get()    
 $.ajaxSettings.async = false;    //执行之前   设置为同步
 $.get("url", data, function(result) {    });
   

//$.getJSON()    
 $.ajaxSettings.async = false;    //执行之前,设置为同步
 $.getJSON("url", data, function(result) {    });
 $.ajaxSettings.async = true;     //可选:执行完后,设置回异步

Second, $ ajax () can set the value of the "async" parameter when requested , as shown in the following example

PS: The default value of async is true (asynchronous), when its value is false, it means synchronization

 
$.ajax({
    type: "post",
    url: "url",
    data: {"name":"tom"},
    async: false, //同步
    success: function(result){
        
    }  
});

 

Published 77 original articles · 100 likes · 70,000+ views

Guess you like

Origin blog.csdn.net/super_DuoLa/article/details/103143052