小程序wx.request用法

XXX.JS:

onLoad: function () {
    this.getTableData();
  }, /*onLoad-end */
  getTableData: function () {//自定义函数名称
    var that = this; 
    // 这个地方非常重要,重置data{}里数据时候setData方法的this应为以及函数的this, 如果在下方的sucess直接写this就变成了wx.request()的this了
    wx.request({
       //请求接口的地址
      url: 'http://api.yytianqi.com/forecast7d?city=39.93,116.40&key=bopi3li1ip93ae0n', 
      data: {
   
      },
      header: {
        "Content-Type": "applciation/json" //默认值
      },
      success: function (res) {
        //res相当于ajax里面的返回的数据
        console.log(res.data);
         //如果在sucess直接写this就变成了wx.request()的this了
      //必须为getTableData函数的this,不然无法重置调用函数
        that.setData({
          datas: res.data  //datas传值给页面的,可以自定义命名
                  })
          },
      fail: function (err) { },//请求失败
      complete: function () { }//请求完成后执行的函数
    })
    
  }

XXX.wxml:

<block wx:for="{{datas}}">  <!--datas不解释  -->
<view>
  {{item.cityId}}   <!--item为必须,且不可修改,别问为什么,官方定义的。  -->                            
</view>
<view>
  {{item.cityName}}
</view>
<view>
  {{item.sj}}
</view>
 </block>
代码分享就到这里了。谢谢大家观赏。推荐一下, 我的群:789826996

猜你喜欢

转载自blog.csdn.net/u011798443/article/details/80581129