小程序的後臺獲取數據方式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'
,//請求地址,用你後臺PHP地址也可以。
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>