新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
微信小程序中request接口如何封裝-創(chuàng)新互聯(lián)
今天小編給大家分享一下微信小程序中request接口如何封裝的相關(guān)知識點,內(nèi)容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
微信小程序 request接口的封裝實例代碼
小程序request接口的封裝(本質(zhì)上是對request回調(diào)函數(shù)再次回調(diào))
module.exports.getData = function (url) { var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var method = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'POST'; var header = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }; return new Promise(function (resolve, reject) { wx.request({ url: url, data: data, method: method, header: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }, success: function (res) { resolve(res) }, fail: function (res) { reject(res) } }) }) }
調(diào)用方法
var common = require("../../common/common.js") common.getData(postUrl) .then(function(data){ this.setData({ //.... }); console.log(data); });
以上就是“微信小程序中request接口如何封裝”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當前文章:微信小程序中request接口如何封裝-創(chuàng)新互聯(lián)
網(wǎng)頁路徑:http://www.ef60e0e.cn/article/cdsiei.html