Home >WeChat Applet >Mini Program Development >Mini program implements post and get encapsulation
This article mainly introduces to you the relevant information on the encapsulation of the post method and the get method in the WeChat applet, so that you can master how to encapsulate it. Friends who need it can refer to it. I hope it can help you.
WeChat applet development post method and get method encapsulation
Step one: Create the httpUtil.js file in the utils folder
The second step: Create the function httpPost method code as follows:
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) } }); };
The third step, add in the module:
module.exports = { httpGet: Get, httpPost: Post };
The fourth step is to introduce
var httpUtil = require('../../utils/HttpHelper.js')
The fifth step is to use
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); }
Related recommendations:
Sharing of code examples for submitting data through post and get methods in Python
A brief analysis of the usage differences between Jquery AJAX POST and GET
The above is the detailed content of Mini program implements post and get encapsulation. For more information, please follow other related articles on the PHP Chinese website!