ajax简单封装

function ajax(type, url, data) {
    return new Promise((resolve, reject) => {
        $.ajax({
            type: type,
            url: url,
            dataType: 'json',
            data: data,
            beforSend: function () {
            },
            error: function (err) {
                reject(err.data)
            },
            success: function (data) {
                resolve(data)
            }
        });
    })
}

调用方法

ajax('post', url, {type: '1'}).then(res => {
    console.log('请求成功')
}).catch(() => {
    console.log('请求失败')
})
原文地址:https://www.cnblogs.com/nixu/p/15134961.html