jq中使用promise封装ajax

let ajax=function(url, param, type = 'GET'){
    const promise = new Promise(function(resolve, reject){
        $.ajax({
            type: type,
            url: url,
            data: param,
            dataType: 'json',
            success(res) {
                resolve(res)
            },
            error(res) {
                reject('响应错误')
                // reject(res.statusText)
            }
        })
    })
    return promise
}

// 使用示例
ajax('http://wh.xhd.cn/api/content/list.jspx',{channelIds: 1}).then(res=>{
    console.log(res)
})

 

posted @ 2019-07-12 11:02  nanacln  阅读(1008)  评论(0编辑  收藏  举报