app.js里的代码
//全局对象httpClient
httpClient: {
request: function (method, url, data) {
//返回一个promise实例
return new Promise((resolve, reject) => {
wx.request({
url: bascUrl+url,
data: data,
mehtod: method,
success: function (res) {
console.log(res)//输出的"POST"
resolve(res)
},
fail: function (res) {
reject(res);
}
})
})
},
//get方法:用来获取数据
get: function (url) {
console.log(1)
return this.request(‘GET’, url);
},
//post方法:用来更新数据
post: function (url, data) {
console.log(2) // 进入POST
return this.request(‘POST’, url, data);
},
//put方法
put: function (url, data) {
return this.request(‘PUT’, url, data);
},
//delete方法
delete: function (url, data) {
return this.request(‘DELETE’, url, data);
}
},
调用代码
formSubmit(e) {
var param = e.detail.value;
param.openid = app.globalData.openId
let cObject = utils.mergeObject(param, app.globalData.userInfo)
app.httpClient.post(‘login/saveWx’, cObject)// 调用
.then(res=>{
console.log(‘form发生了submit事件,携带数据为:’)
})
.catch(res=>{})
},
请求结果:
我在Promise封装了wx.request后request method出错,不管GET方法还是POST方法,服务器接收到的request method前面都带有之前请求的参数
https://developers.weixin.qq.com/community/develop/doc/00064cbd2601d8f26d98b822856400