wx.request
微信小程序自身具备发起网络请求的API,wx.request,参数详见 https://developers.weixin.qq.com/miniprogram/dev/api/network/request/wx.request.html
wx.request({
url: 'example.php', //仅为示例,并非真实的接口地址
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json' // 默认值
},
success (res) {
console.log(res.data)
}
})
注:小程序必须使用 HTTPS/WSS 发起网络请求。需要事先设置通讯域名,小程序只可以跟指定的域名进行网络通信。
封装思想
通过原始success成功的回调函数和fail失败的回调