• ES6中Promise封装ajax的写法


    1.依赖jquery的写法可以使用下面的
        (function($){
            $.extend({
                ajaxPromise:  param => {
                    return new Promise((resolve, reject) => {
                        $.ajax({
                            method: param.method || "GET",
                            dataType: "JSON",
                            url: param.url,
                            data: param.data || "",
                            success: res => {
                                if (res.status == 1) {
                                    resolve(res.data);
                                }
                                else if (res.status == -1001) {
                                    console.log("需要登录");
                                }
                            },
                            error: err => {
                                if (err.status == 404) {
                                    console.log("404");
                                } else {
                                    reject(err);
                                }
                            }
                        })
                    })
                }
            })
        })(jQuery);
    

    2.如果不依赖jquery可以采用下面的写法

    const ajaxPromise =  param => {
            return new Promise((resolve, reject) => {
                $.ajax({
                    type: param.type || "get",
                    url: param.url,
                    data: param.data || "",
                    success: res => {
                        if(res.status == 1){
                            resolve(res.data);
                        }
                        else if(res.status == -1001){
                            alert("需要登录");
                        }else{
    
                        }
                    },
                    error: err => {
                        if(err.status == 404){
                            alert("404");
                        }else{
                            reject(err);
                        }
                    }
                })
            })
        };
    

      

    使用jquery示例

      /*
         第一个请求
         */
        let step1 = () => {
            $.ajaxPromise({
                url:"agents/get/2068957732939648",
                data: {
                    "token":"f59e423eb75503f5609a0b9ba3b38db8083ad65078edb945b4c528fe6440b728"
                }
            }).then(res => {
                console.log("第一个请求正确返回==>"+res);
            }).catch(err => {
                console.log("第一个请求失败",err);
            })
        };
    
        /*
         第二个请求
         */
        let step2 = () => {
            $.ajaxPromise({
                url:"http://staging.santezjy.com:1031/agents/get/2068957732939648",
                data: {
                    "token":"f59e423eb75503f5609a0b9ba3b38db8083ad65078edb945b4c528fe6440b728"
                }
            }).then(res => {
                console.log("第二个请求正确返回==>",res);
            }).catch(err => {
                console.log("第二个请求失败==>"+err);
            })
        };
    
        step1();
        step2();
    

      

  • 相关阅读:
    雷少东_百度百科
    180China丨the Agency for Brand Engagement and Experience
    建立可信连接
    【行业干货】2013中国零售商排名
    30万左右买什么车好?_百度知道
    极速入职-拉勾网-专注互联网职业机会
    python
    关于幂律分布的一个笔记_哈克_新浪博客
    幂律分布_百度百科
    使用firefox直接 打开pdf文件可以破解禁止打印的功能
  • 原文地址:https://www.cnblogs.com/tonnytong/p/10983246.html
Copyright © 2020-2023  润新知