微信小程序--后台交互/wx.request({})方法/渲染页面方法 解析

来源:互联网 发布:软件产品说明书怎么写 编辑:程序博客网 时间:2024/05/21 23:32

小程序的后台获取数据方式get/post具体函数格式如下:wx.request({})

wxml:

<view  wx:for="{{logs}}" wx:for-item="value">  {{value.catname}}</view>
js:

data: {    logs:[]  },onLoad:function(){this.getdata(); }getdata:function(){//定义函数名称var that=this;   // 这个地方非常重要,重置data{}里数据时候setData方法的this应为以及函数的this, 如果在下方的sucess直接写this就变成了wx.request()的this了    wx.request({      url:'http://www.phonegap100.com/appapi.php?a=getPortalCate',//请求地址      data:{//发送给后台的数据        name:"bella",        age:20      },      header:{//请求头        "Content-Type":"applciation/json"      },      method:"GET",//get为默认方法/POST      success:function(res){        console.log(res.data);//res.data相当于ajax里面的data,为后台返回的数据      that.setData({//如果在sucess直接写this就变成了wx.request()的this了.必须为getdata函数的this,不然无法重置调用函数       logs:res.data.result           })       },      fail:function(err){},//请求失败      complete:function(){}//请求完成后执行的函数    })  }


页面结果:



转载及分享标明出处:http://www.cnblogs.com/bellagao/ 作者:高辉彩


 
阅读全文
0 0