小程式請求(get+post)
1,預設get請求
wx.request({
url: 'test.php', //僅為示例,並非真實的介面地址
data: { x: '' , y: '' },
header: {
'content-type': 'application/json' // 預設值
},success: function(res) {
console.log(res.data)
}
})
2,post請求
wx.request( {
url: " ",
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST",
//data: { cityname: "上海", key: "1430ec127e097e1113259c5e1be1ba70" },
data: Util.json2Form( { cityname: "上海", key: "1430ec127e097e1113259c5e1be1ba70" }),
complete: function( res ) {
that.setData( {
toastHidden: false,
toastText: res.data.reason,
city_name: res.data.result.data.realtime.city_name,
date: res.data.result.data.realtime.date,
info: res.data.result.data.realtime.weather.info,
});
if( res == null || res.data == null ) {
console.error( '網路請求失敗' );
return;
}
}
})