今天学习了一下微信小程序的入门开发,在使用网络请求时,发现根据微信官方的API的方法进行操作出现Invalid request 400错误,到底怎么回事呢?
首先我们来看微信API网络请求 示例代码: 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) } })但是发现会出现400错误。
错误提示如下所示:
这是怎么回事呢?
后来发现,微信开发者工具在更新到最新版本后(我现在使用的版本是0.14.140900),相应的参数配置也发生了变化,官网给出的这个配置已经不能用了,需要改为'Content-Type': 'json' 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) } })结果如下: