微信小程序网络请求封装
封装,utils/http.js文件
export default function ajax(type, url, data) {
return new Promise((resolve, reject) => {
wx.request({
url: url,
method: type,
header: {
'content-type': 'application/json'
},
data: data,
success: (res) => {
resolve(res.data)
},
fail: (res) => {
reject(res)
}
})
})
}
调用
import ajax from '../utils/http.js'
ajax(type, url, data).then(res => { //成功时执行
}).catch(function () { //失败时执行
}).finally(function () { //不管成功或失败时都会执行
})