天天看點

微信小程式接口伺服器,微信小程式--調用外部伺服器接口

調用外部伺服器接口

功能:

調用wx.request方法,通路https的API接口,傳回資料。

首先,要在微信小程式背景,添加要通路的域名。

配置好域名後,就可以寫調用代碼了。

對應的.js檔案

注意1:

配置完域名後,在開發者工具中重新整理一下,确定其已經存在了。

微信小程式接口伺服器,微信小程式--調用外部伺服器接口

注意2:

在通路的時候,添加一個header,否則無法傳回資料。(參照着網上找到的答案,但是不明白為什麼)

注意3:

要想在wx.request中,向data中添加資料,要先重新定義變量this

var that = this;

wx.request({

url: 'https://www.rest-time.top/api/select_search',

method: 'POST',

header: {

'content-type': 'application/x-www-form-urlencoded'

},

data: {

openid: uuid,

},

dataType: 'json',

responseType: 'text',

success: function(res) {

// 不能直接用this.setData

that.setData({

arrived: res.data

})

}

})

感覺有用的小夥伴,點個贊再走撒~