解决·微信小程序开发-网络请求报Invalid request 400错误

来源:互联网 发布:淘宝优质网店怎么搜 编辑:程序博客网 时间:2024/06/15 05:22

今天学习了一下微信小程序的入门开发,在使用网络请求时,发现根据微信官方的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)        }    })

结果如下:

这里写图片描述

1 0
原创粉丝点击