$.ajaxSettings.async = false 讲ajax设置为同步

我们都知道$.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;

posted @ 2022-02-15 23:17  曲琦  阅读(274)  评论(0编辑  收藏  举报