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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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 (){} //请求完成后执行的函数
     })
   },
  wxml页面:
 
<view  wx: for = "{{logs}}"  wx: for -item= "value" >
   {{value.catname}}
</view>

  

页面结果:

转载于:https://www.cnblogs.com/frx666/p/6591256.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值