在大部分项目中,轮播图都是必不可少的。接下来通过轮播图这一实例来学习微信小程序中的数据请求。
一、网络请求的学习
1. 基本使用
微信提供的专属的API接口,用于网络请求:wx.request(Object object)
- 官方文档:
https://developers.weixin.qq.com/miniprogram/dev/api/network/request/wx.request.html
- 注意事项:
https://developers.weixin.qq.com/miniprogram/dev/framework/ability/network.html
2. 关键属性解析
- url:必传(不然请求什么呢)
- data:请求参数
- method:请求的方式
- succes:成功时的回调
- fail:失败时的回调
3. 网络请求的三种基本方式 - 原生方式
在某一页面中的 .js文件中,onLoad属性中发送网络请求
(1)发送最简单的get请求
Page({
onLoad:function (options){
wx.request({
url: 'http://httpbin.org',
success:function(res){
//回调函数
console.log(res)
}
})
}
})
(2)get请求,并且携带参数
Page({
onLoad:function (options){
wx.request({
url: 'http://123.207.32.32:8000/home/data',
data:{
type:'sell',
page:1
},
success:function(res){
console.log(res)
}
})
}
})
(3)post请求,并且携带参数
Page({
onLoad:function