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

小程序的後臺獲取數據方式get/post具體函數格式以下:wx.request({})javascript

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>

  

頁面結果:php

相關文章
相關標籤/搜索