微信小程序开发中的网络请求和数据获取是非常重要的内容,本文将通过代码案例的方式详细介绍相关知识点。
- 发送GET请求
wx.request({
url: 'https://api.example.com/data',
method: 'GET',
success: function (res) {
console.log(res.data);
},
fail: function (error) {
console.log(error);
}
});
代码解释:
- 使用
wx.request
函数发送一个GET请求,将请求的URL设置为https://api.example.com/data
。 success
函数是请求成功后的回调函数,其中的res.data
即为请求返回的数据。fail
函数是请求失败后的回调函数,其中的error
即为请求失败的原因。
- 发送POST请求
wx.request({
url: 'https://api.example.com/data',
method: '