小程序的后台獲取數據方式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>
頁面結果:

