微信小程序 網絡請求 GET
微信小程序開發中網絡請求必不可少,今天說說最簡單的請求.后續會嘗試上傳下載,Socket這些.
1.一個微信小程序,同時只能有5個網絡請求連接。
這個規定應該是微信為了保證用戶體驗制定的,畢竟是小程序.
2.wx.request(OBJECT) 參數說明:

微信小程序支持GET,POST等請求.用method可以設置.
以下是GET請求的代碼:
<span style="font-size:18px;">//rate.js //獲取應用實例 var app = getApp() Page( { data: { code: 'USD', currencyF_Name: '', currencyT_Name: '', currencyF: '', currencyT: '', currencyFD: 1, exchange: 0, result: 0, updateTime: '', }, onLoad: function( options ) { var that = this; //獲取匯率 wx.request( { url: "http://op.juhe.cn/onebox/exchange/currency?key=我的appkey&from=CNY&to="+code, success: function( res ) { that.setData( { currencyF_Name: res.data.result[0].currencyF_Name, currencyT_Name: res.data.result[0].currencyT_Name, currencyF: res.data.result[0].currencyF, currencyT: res.data.result[0].currencyT, currencyFD: res.data.result[0].currencyFD, exchange: res.data.result[0].exchange, result: res.data.result[0].result, updateTime: res.data.result[0].updateTime, }) } }) } })</span> 上面代碼中只需要給出URL即可,onLoad函數在頁面初始化時啟動,wx.request({})中success的res.data是從后臺獲取的數據,這一點需要注意.
以下是獲取的json數據的格式.

json的解析都不需要自己做了.我做Android的時候還得用gson或者是fastjson來解析json.微信為我們解決了很多麻煩.
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答