您的位置:首页 > 移动开发 > 微信开发

微信小程序开发(1)----post方法与get方法的封装

2017-07-29 15:22 585 查看
第一步:在utils文件夹下创建httpUtil.js文件

第二步:创建函数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);
}


—————————————————————————————————————————————————–

java 架构师全套教程,共760G, 让你从零到架构师,每月轻松拿3万

下载地址:

https://item.taobao.com/item.htm?spm=686.1000925.0.0.4a155084hc8wek&id=555888526201


01.高级架构师四十二个阶段高

02.Java高级系统培训架构课程148课时

03.Java高级互联网架构师课程

04.Java互联网架构Netty、Nio、Mina等-视频教程

05.Java高级架构设计2016整理-视频教程

06.架构师基础、高级片

07.Java架构师必修linux运维系列课程

08.Java高级系统培训架构课程116课时

(送:hadoop系列教程,java设计模式与数据结构, Spring Cloud微服务, SpringBoot入门)

—————————————————————————————————————————————————–
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息