今天學(xué)習(xí)了一下微信小程序的入門開發(fā)纯续,在使用網(wǎng)絡(luò)請求時随珠,發(fā)現(xiàn)根據(jù)微信官方的API的方法進(jìn)行操作出現(xiàn)Invalid request 400錯誤,到底怎么回事呢猬错?
首先我們來看微信API網(wǎng)絡(luò)請求 示例代碼:
wx.request({
url: 'test.php', //僅為示例,并非真實的接口地址
data: {
x: '' ,
y: ''
},
header: {
'content-type': 'application/json'
},
success: function(res) {
console.log(res.data)
}
})
我項目中的代碼
wx.request({
url: 'https://api.douban.com/v2/movie/in_theaters', //僅為示例倦炒,并非真實的接口地址
data: {},method:'get', header:{
'content-type': 'application/json'
}, success:function(res){
console.log(res.data)
}})
但是發(fā)現(xiàn)會出現(xiàn)400錯誤显沈。
錯誤提示如下所示:
20170312224351091.png
這是怎么回事呢?
后來發(fā)現(xiàn)魔慷,微信開發(fā)者工具在更新到最新版本后(我現(xiàn)在使用的版本是0.14.140900),相應(yīng)的參數(shù)配置也發(fā)生了變化邀摆,官網(wǎng)給出的這個配置已經(jīng)不能用了纵顾,需要改為'Content-Type': 'json'
001.png
wx.request({
url: 'https://api.douban.com/v2/movie/in_theaters', //僅為示例,并非真實的接口地址
data: {},method:'get', header:{
// 'content-type': 'application/json'
'Content-Type': 'json'
}, success:function(res){
console.log(res.data)
}})
結(jié)果如下:
002.png