网络通信

https://developers.weixin.qq.com/ebook?action=get_post_info&docid=000ee27c9c8d98ab0086788fa5b00a

4.4 发起 https 网络通信
小程序经常需要往服务器传递数据或者从服务器拉取信息,
这个时候可以使用wx.request这个API。

wx.request({

  url: 'https://test.com/getinfo',

  success: function(res) {

    console.log(res)// 服务器回包信息

  }

})

向服务器发送请求有两种方法
get, 发送给服务器的数据在 url 里
post, 发送给服务器的数据在 wx.request 的 data 参数里
url 长度有限制,所以一般用 post 方法
传送复杂数据时用 JSON 格式更合适, header 要做设置
header: { ‘content-type’: ‘application/json’},

wx.request详细参数

具体见
https://developers.weixin.qq.com/ebook?action=get_post_info&docid=000ee27c9c8d98ab0086788fa5b00a

03-22 12:49