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


小程序的后台獲取數據方式get/post具體函數格式如下:wx.request({})

 

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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM