微信小程序-发起 HTTPS 请求

该api属于基础,因为我们在开发时,要调用自己服务器或者别的服务器上的接口。

比如,我现在要进行注册,用户提交表单,要向服务器发请求进行注册。

下面时程序实例

wx.request({
  url: 'test.php', //仅为示例,并非真实的接口地址
  data: {
     x: '' ,
     y: ''
  },
  header: {
      'content-type': 'application/json'
  },
  success: function(res) {
    console.log(res.data)
  }
})
这个接口对于json格式的数据接口时非常方便的,而且不用考虑跨域问题的

data 数据说明 最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String 。转换规则如下:

  • 对于 header['content-type'] 为 'application/json' 的数据,会对数据进行 JSON 序列化
  • 对于 header['content-type'] 为 'application/x-www-form-url-encoded' 的数据,会将数据转换成 query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...
原文地址:https://www.cnblogs.com/tenyear/p/wx_http.html