Jquery AJAX如何使用Promise/Deferred实现顺序执行? - shenggen - 博客园

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

?

1

2

3

4

5

6

7

8

9

$.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 ,同时请求多个

?

1

2

3

4

5

6

7

8

function successFunc(){ console.log( “success!” ); }  

function failureFunc(){ console.log( “failure!” ); }  

$.when(  

$.ajax( "/main.php" ), 

$.ajax( "/modules.php" ), 

$.ajax( “/lists.php” ) 

).then( successFunc, failureFunc );  


Original url: Access
Created at: 2020-06-22 13:46:32
Category: default
Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论