$.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;

本文作者:曲琦

本文链接:https://www.cnblogs.com/123525-m/p/15898547.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   曲琦  阅读(283)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起