zoukankan      html  css  js  c++  java
  • 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 );   
    

      

  • 相关阅读:
    HTML和CSS 基本要点必看
    CSS
    六个选择器
    HTML初级课程 (自学可懂)
    this
    1.作用域链
    js if 语句
    js数据类型
    ifelse语句
    三元运算符
  • 原文地址:https://www.cnblogs.com/shenggen/p/5104879.html
Copyright © 2011-2022 走看看