小編給大家分享一下微信小程序中post方法與get方法如何封裝,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
微信小程序開(kāi)發(fā)post方法與get方法的封裝
第一步:在utils文件夾下創(chuàng)建httpUtil.js文件
第二步:創(chuàng)建函數(shù)httpPost方法代碼如下:
function Post(url, data, cb, isShow, showNetError, that, showLoading) { if (showLoading == true || showLoading == undefined){ wx.showNavigationBarLoading(); wx.showLoading({ title: '加載中...', }) } var basicData = { vloginPwd: api.vloginPwd, vtoken: api.vtoken } if (!isEmpty(data)) { for (var key in data) { try { basicData[key] = data[key]; } catch (e) { } } } wx.request({ url: url, header: { 'content-type': 'application/x-www-form-urlencoded' }, method: 'POST', data: basicData, success: (res) => { if (res.data.state == 200) { typeof cb == "function" && cb(res.data, ""); } else { if (isShow == true) { wx.showModal({ title: '提示', content: res.data.msg, showCancel: false }) } } }, fail: (err) => { if (showNetError) { that.setData({ errorDisplay:'', containHidden:true }) } }, complete: (res) => { setTimeout(function () { wx.hideNavigationBarLoading(); wx.hideLoading(); }, 100) } }); };
第三步,在module里添加:
module.exports = { httpGet: Get, httpPost: Post };
第四步,引入
var httpUtil = require('../../utils/HttpHelper.js')
第五步,如何使用
onload:function(option){ var that = this; httpUtil.httpPost(api.getListUrl, jsonData, function (res) { wx.showModal({ title: '提示', content: res.msg, showCancel: false, confirmText:"查看", success: function (res) { console.log("res.data===", res.data); if (res.confirm) { that.toDetail(res.data); } } }) }, true, true, this); }
以上是“微信小程序中post方法與get方法如何封裝”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文標(biāo)題:微信小程序中post方法與get方法如何封裝-創(chuàng)新互聯(lián)
文章路徑:http://aaarwkj.com/article28/cdhjcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、App設(shè)計(jì)、搜索引擎優(yōu)化、網(wǎng)站策劃、移動(dòng)網(wǎng)站建設(shè)、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容