微信小程序接口封装
function baseRequest({ url, method, header, data }, resolve, reject) { wx.request({ url, method, header, data, success: function (res) { // 需要判断服务器code的用这一段 // 返回0表示真正的成功,其他code表示各种错误码 if (res.data.code === 0) { resolve(res) } else { reject(res) } resolve(res) }, fail: function (res) { reject(res) } }) }
使用promise
function requestPromise(options) { let req = new Promise((resolve, reject) => { baseRequest(options, resolve, reject) }) return req }
get方法
function get(options) { options.method = ‘GET‘ return requestPromise(options) }
post方法
function post(options) { options.method = ‘POST‘ if (!options.header) { options.header = {} } options.header["Content-Type"] = "application/json" return requestPromise(options) }
put方法
function put(options) { options.method = ‘PUT‘ if (!options.header) { options.header = {} } options.header["Content-Type"] = "application/json" return requestPromise(options) }
相关推荐
kgshuo 2020-09-25
Tomato 2020-09-10
taiyangyu 2020-09-10
CodeAndroid 2020-09-10
small 2020-07-29
sucheng 2020-07-26
zuoliangzhu 2020-07-20
CodeAndroid 2020-07-14
xiaoxubbs 2020-07-04
sucheng 2020-06-25
kgshuo 2020-06-14
意外金喜 2020-06-14
zuoliangzhu 2020-06-14
tianping 2020-06-14
hgzhang 2020-06-14
killgod 2020-06-14
戴翔的技术 2020-06-14
郴州小程序 2020-06-13