$.ajaxSettings.async = false

我们都知道$.post() 和 $.get()都是异步请求的方式,但有些时候却需要用到同步请求。

方式一、

/设置为同步
 $.ajaxSettings.async = false;
  $.post("url", data, function(result) {
   // 请求处理
  },"json");
  //设置回异步
 $.ajaxSettings.async = true; 

注意:使用这种方式,使用了同步后($.ajaxSettings.async = false;),需及时释放掉使用异步($.ajaxSettings.async = true;),不然会锁死资源,使得其他线程不能访问数据;

方式二、

$.ajax({
    type: "post",
    url: "url",
    data: {"reportId": rows[0].reportId},
    async: false,
    success: function(result){
        if (result.success){}else{} 
    }  
});

同步$.ajaxSettings.async = false;

异步$.ajaxSettings.async = true;

原文地址:https://www.cnblogs.com/zouhong/p/13084329.html