Ajax请求的async属性简介

官网API地址:http://api.jquery.com/jQuery.ajax/

async Boolean Default: true 

By default, all requests are sent asynchronous (e.g. this is set to true by default). If you need synchronous requests, set this option to false. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.

async:默认值为:true;默认为异步方式执行,$.Ajax执行后,在还没执行结束之前就继续执行后面的ajax脚本,知道服务器返回数据,此时会触发ajax的success方法,此时就会有多个线程在执行。

async:值为:false;当前ajax为同步请求。在当前ajax没有返回值之前,同步请求将锁住浏览器,其他操作都必须的等到当前ajax执行完毕,才可以执行。

Demo:

$.ajax({

          cache:false,

          async:false,

          type:"GET",

          data:{zdnm:zdnm},

          success:function(data){

                  var row = data.content;

                   .....

                  if(row){

                        cache:false,

                        async:false,

                        type:"POST",

                        data{param1:param1,param2:param2},

                        success:function(data){

                                         var row = data.content;

                                          .....

                               }

                  }

           }

});

注意:嵌套ajax的使用是使用async属性最常用的地方:

async:默认值: true。默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为 false。注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行。

猜你喜欢

转载自blog.csdn.net/coding_1994/article/details/84347481