微信小程序中post方法與get方法的封裝
更新時間:2017年09月26日 10:07:43 作者:CharlinGod
這篇文章主要介紹了微信小程序中post方法與get方法的封裝的相關(guān)資料,希望通過本文能幫助到大家,讓大家掌握如何封裝,需要的朋友可以參考下
微信小程序開發(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);
}
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望通過本文能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Javascript & DHTML DOM基礎和基本API
DOM是文檔對象模型(Document Object Model,是基于瀏覽器編程(在本教程中,可以說就是DHTML編程)的一套API接口,W3C出臺的推薦標準,每個瀏覽器都有一些細微的差別,其中以Mozilla的瀏覽器最與標準接近。2008-07-07
JavaScript本地數(shù)據(jù)存儲sessionStorage與localStorage使用詳解
這篇文章主要介紹了JavaScript本地數(shù)據(jù)存儲sessionStorage與localStorage使用,localStorage:永久存儲在本地,適合保存在本地的數(shù)據(jù)。sessionStorage:會話級的存儲,敏感帳號一次登陸2022-10-10
JS解決IOS中拍照圖片預覽旋轉(zhuǎn)90度BUG的問題
下面小編就為大家?guī)硪黄狫S解決IOS中拍照圖片預覽旋轉(zhuǎn)90度BUG的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09

