前言:这两个问题(设置‘Authorization’ 与 请求400)是没有任何关系的的,但是由于我是一起处理的,就直接一起写在这里了
网络请求:
wx.request({
url: 'url.url', // 仅为示例,并非真实的接口地址
header:{
'Authorization': 'Bearer ' + token
},
success(res){
console.log(res)
},
fail(error){}
})
需要了解:
微信官方文档:https://developers.weixin.qq.com/miniprogram/dev/api/network/request/wx.request.html
data 参数说明:
最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String 。转换规则如下:
对于 GET 方法的数据,会将数据转换成 query string(encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)
对于 POST 方法且 header['content-type'] 为 application/json 的数据,会对数据进行 JSON 序列化
对于 POST 方法且 header['content-type'] 为 application/x-www-form-urlencoded 的数据,会将数据转换成 query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)
解决:
//我们需要把header修改一下:
header:{
// "Content-Type":"application/json"
// 修改为:
"Content-Type": "application/text"
},