本文編輯: toBeMN瀏覽 36243
版權(quán)所有,嚴(yán)禁轉(zhuǎn)載
wx.request是小程序客戶端與服務(wù)器端交互的接口(類似javascript的ajax請(qǐng)求),只能發(fā)起 HTTPS 請(qǐng)求。一個(gè)微信小程序,同時(shí)只能有5個(gè)網(wǎng)絡(luò)請(qǐng)求連接,因此開(kāi)發(fā)者要控制好請(qǐng)求的數(shù)量。由于request操作依賴網(wǎng)絡(luò),會(huì)造成等待,影響用戶體驗(yàn),因此目前只支持異步模式。
需注意:
客戶端的 HTTPS TLS 版本為1.2,但 Android 的部分機(jī)型還未支持 TLS 1.2,所以請(qǐng)確保 HTTPS 服務(wù)器的 TLS 版本支持1.2及以下版本
請(qǐng)求參數(shù) method 的 value 經(jīng)測(cè)試(IDE和IOS)不區(qū)分大小寫(xiě),默認(rèn)為GET請(qǐng)求
小程序后臺(tái)(不是接口的后臺(tái))配置的 URL 中不能帶有端口號(hào),并且不能使用localhost/127.0.0.1
,想要在本地使用 localhost 進(jìn)行測(cè)試,方法詳見(jiàn)下方提供的鏈接
利用request請(qǐng)求訪問(wèn)微信小程序俱樂(lè)部提供的API
客戶端展示
服務(wù)器端展示
【代碼】
wxml
<view class="container">
<button hover-class="but-hover" bindtap="insert">插入數(shù)據(jù)</button>
<button hover-class="but-hover" bindtap="update">修改數(shù)據(jù)</button>
<button hover-class="but-hover" bindtap="del">刪除數(shù)據(jù)</button>
<text>key:{{aKey}}
value:{{aValue}}
type:{{aType}}</text>
</view>
js
var app = getApp()
Page({
data:{
aKey:"null",
aValue:"null",
aType:"null"
},
onLoad:function(options){
//加載時(shí)就獲取后臺(tái)的數(shù)據(jù)
this.get_data()
},
insert:function(){
// 插入數(shù)據(jù)
var that=this
wx.request({
url: 'https://api.wxappclub.com/put',
data: {
appkey: 'hi0yhmmemhkn00ud7ne748agga7qyj1j' ,
key: "akey",
value:"avalue",
type:"String"
},
method:'get',
header: {
'Content-Type': 'application/json'
},
success: function(res) {
that.get_data();
}
})
},
update:function(){
// 更新數(shù)據(jù)
var that=this
wx.request({
url: 'https://api.wxappclub.com/put',
data: {
appkey: 'hi0yhmmemhkn00ud7ne748agga7qyj1j' ,
key: "akey",
value:"new_avalue",
type:"String"
},
method:'get',
header: {
'Content-Type': 'application/json'
},
success: function(res) {
that.get_data();
}
})
},
del:function(){
//刪除數(shù)據(jù)
var that=this
wx.request({
url: 'https://api.wxappclub.com/del',
data: {
'appkey': 'hi0yhmmemhkn00ud7ne748agga7qyj1j' ,
'key': "akey",
'type':"String"
},
method:'get',
header: {
'Content-Type': 'application/json'
},
success: function(res) {
that.setData({
aKey:"null",
aValue:"null",
aType:"null"
})
}
})
},
get_data:function(){
// 獲取數(shù)據(jù)
var that=this;
wx.request({
url: 'https://api.wxappclub.com/get',
data: {
'appkey': 'hi0yhmmemhkn00ud7ne748agga7qyj1j',
'key': "akey",
'type':"String"
},
header: {
'content-type': 'application/json'
},
success: function(res) {
if(res.data.success){
that.setData({
aKey:res.data.result.key,
aValue:res.data.result.value,
aType:res.data.result.type
})
}
}
})
}
})
wxss
page{
height: 100%;
width:100%;
}
.container{
display: flex;
flex-direction: column;
align-items: center;
}
button{
width:500rpx;
margin: 10rpx 0;
}
.but-hover{
background-color: #5CB85C;
}
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
url | String | 是 | 請(qǐng)求后臺(tái)接口的地址(不能帶有端口號(hào)) |
data | Object、String | 否 | 請(qǐng)求攜帶的參數(shù) |
header | Object | 否 | 設(shè)置請(qǐng)求的 header , header 中不能設(shè)置 Referer |
method | String | 否 | 默認(rèn)為 GET,有效值:OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT |
success | Function | 否 | 請(qǐng)求后臺(tái)成功返回后的回調(diào)函數(shù),res = {data: ‘開(kāi)發(fā)者服務(wù)器返回的內(nèi)容’} |
fail | Function | 否 | 接口調(diào)用失敗的回調(diào)函數(shù) |
complete | Function | 否 | 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行) |