promise

作者: 无名小码农 | 来源:发表于2018-07-25 09:24 被阅读0次

方法一 不用promise

//外层ajax,校验实名信息$.ajax({

   type: "POST",

   url: url,

   dataType:"json",

   data: {name:"张三",age:"43"},

   success: function(msg){    

     if(msg.status)

     {        //成功,提交表单数据到本系统后台

        $.ajax({

           type: "POST",

           url: "/my/info.php", //本系统后台地址

           dataType:"json",

           data: {表单数据},

           success: function(msg){    

             if(msg.success)

             {                //保存成功             

             }             else

             {                //保存失败

             }

           }

        });

     }     else

     {        //错误

     }

   }

});

方法二 使用promise

new Promise(function(resolve, reject) {

           $.ajax({

               type: "POST",

               url: url,

               dataType: "json",

               data: { name: "张三", age: "43"},

               success: function(msg) {

                   if(msg.status) {

                       resolve(msg); //通过验证,msg会传入then方法的第一个方法参数

                   }                    else{

                       reject(msg); //未通过验证,msg会传入then方法的第二个方法参数

                   }

               }

           });

       }).then(function(resolveMsg) {

           $.ajax({

               type: "POST",

               url: "/my/info.php", //本系统后台地址

               dataType: "json",

               data: { "表单数据": "表单数据"},

               success: function(msg) {

                   if(msg.success) {                        //保存成功             

                   }                    else{                        //保存失败

                   }

               }

           });

       }

       , function(rejectMsg) {

           //错误

       });

相关文章

网友评论

      本文标题:promise

      本文链接:https://www.haomeiwen.com/subject/cgwumftx.html