问题描述
- 目的:希望执行完ajax请求之后,再执行后续操作。
- 方案1:$.ajax()中设置async:true;
- 造成的问题:请求过程中页面静止,例如进度条无法显示。
- 解决方案:利用$.when().done()实现
$.when().done()的示例代码
原文链接:https://blog.csdn.net/qq_34369022/article/details/79412627
$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ).done(function( a1, a2 ) {
// a1 and a2 are arguments resolved for the page1 and page2 ajax requests, respectively.
// Each argument is an array with the following structure: [ data, statusText, jqXHR ]
var data = a1[ 0 ] + a2[ 0 ]; // a1[ 0 ] = "Whip", a2[ 0 ] = " It"
if ( /Whip It/.test( data ) ) {
alert( "We got what we came for!" );
}
});
a1是第一个异步返回的数据
a2是第二个异步返回的数据