Encapsulation of post method and get method in WeChat applet

不言
Release: 2018-06-27 15:23:22
Original
2664 people have browsed it

This article mainly introduces the relevant information about the packaging of post method and get method in WeChat applet. I hope this article can help everyone and let everyone know how to encapsulate. Friends in need can refer to it

WeChat applet development post method and get method encapsulation

The first step: Create the httpUtil.js file in the utils folder

The second step: The code to create the function httpPost method is 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)  
  }
 });
};
Copy after login

The third step is to add in the module:

module.exports = {
 httpGet: Get,
 httpPost: Post 
};
Copy after login

The fourth step, introduce

var httpUtil = require('../../utils/HttpHelper.js')
Copy after login

The fifth step, how 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);
}
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Network requests in WeChat applet (post request and get request)

WeChat applet Sequential execution of promsie.all and promise

Introduction to the encapsulation of the WeChat applet request interface

The above is the detailed content of Encapsulation of post method and get method in WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template