微信小程序的ajax数据请求wx.request

使用小程序官方提供的数据请求api发起数据请求

wx.request(OBJECT)

wx.request发起的是https请求。一个微信小程序,同时只能有5个网络请求连接。

OBJECT参数说明:

参数名类型必填说明
urlString开发者服务器接口地址
dataObject、String请求的参数
headerObject设置请求的 header , header 中不能设置 Referer
methodString默认为 GET,有效值:OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
successFunction收到开发者服务成功返回的回调函数,res = {data: '开发者服务器返回的内容'}
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

示例代码:


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);
    })


  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值