微信小程序的ajax数据请求wx.request
2018-07-25 10:44
267 查看
使用小程序官方提供的数据请求api发起数据请求
wx.request(OBJECT)
wx.request发起的是https请求。一个微信小程序,同时只能有5个网络请求连接。
OBJECT参数说明:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
url | String | 是 | 开发者服务器接口地址 |
data | Object、String | 否 | 请求的参数 |
header | Object | 否 | 设置请求的 header , header 中不能设置 Referer |
method | String | 否 | 默认为 GET,有效值:OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT |
success | Function | 否 | 收到开发者服务成功返回的回调函数,res = {data: '开发者服务器返回的内容'} |
fail | Function | 否 | 接口调用失败的回调函数 |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例代码:
-
wx.request({
-
url: 'test.php',
-
data: {
-
x: '' ,
-
y: ''
-
},
-
header: {
-
'Content-Type': 'application/json'
-
},
-
success: function(res) {
-
console.log(res.data)
-
}
-
})
微信小程序中使用fetch做ajax请求
fetch是一种新的ajax请求规范,经懒人建站测试,fetch在小程序中也是支持的,测试ajax请求代码如下:
then中带代码是测试,这里是节选了小部分代码,实际使用需要自行修改。
-
fetch('http://www.51xuediannao.com/json.php?typeid=34&page=1&pagesize=10')
-
.then(function(response){
-
if(response.status==200){
-
that.data.page++;
-
return response.json();
-
}
-
}).then(function(data){
-
console.log(data);
-
//更新数据
-
that.setData({
-
listArr:that.data.page==1 ? data : that.data.listArr.concat(data)
-
})
-
console.log(that.data.listArr);
-
})
相关文章推荐
- 微信小程序的ajax数据请求wx.request
- 微信小程序的ajax数据请求wx.request
- 微信小程序wx.request请求服务器json数据并渲染到页面
- 微信小程序wx.request请求回来的数据没有及时显示到页面上
- 微信小程序wx.request请求用POST后台得不到传递数据
- 微信小程序:wx.request之post请求后端无法获取数据的问题
- 微信小程序- wx.request请求不到数据
- 微信小程序数据请求方法wx.request
- 微信小程序--request(ajax)请求
- 微信小程序的网络设置,及网络请求:wx.request(OBJECT)
- 微信小程序中wx.request返回不了json数据
- windows2008 phpstudy https证书配置实现微信小程序wx.request发起的 HTTPS 请求。
- 微信小程序ajax实现请求服务器数据,模版遍历数据
- 微信小程序-wx.request获取数据
- 微信小程序学习:发起请求wx.request的使用
- 微信小程序ajax请求绑定数据简单实例
- 微信小程序wx.request实现后台数据交互功能分析
- 微信小程序wx.request后台数据交互
- 微信小程序网络请求wx.request请求