Jquery AJAX如何使用Promise/Deferred实现顺序执行?

有的时候有我有N个AJAX请求,第下个请求可能要依赖上个请求的返回值, 可以用

$.ajax("test1.php").then(function(data) {
// data 是 test1.php 的返回值
return $.ajax("test2.php");
}).then(function(data) {
// data 是 test2.php 的返回值
return $.ajax("test3.php");
}).then(function(data) {
return $.ajax("test4.php");
});

  有的时候可以用when ,同时请求多个

function successFunc(){ console.log( “success!” ); }   
function failureFunc(){ console.log( “failure!” ); }   
  
$.when(   
   $.ajax( "/main.php" ),  
   $.ajax( "/modules.php" ),  
   $.ajax( “/lists.php” )  
 ).then( successFunc, failureFunc );   

  

原文地址:https://www.cnblogs.com/shenggen/p/5104879.html