Home  >  Article  >  WeChat Applet  >  Code analysis of request encapsulation in WeChat applet

Code analysis of request encapsulation in WeChat applet

不言
不言Original
2018-08-14 10:36:333144browse

This article introduces to you the code analysis of request encapsulation in WeChat applet. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

request

Only use POST, only encapsulate POST, wait until it is useful GET Then rewrite, the interface uses ThinkPHP5.0

Main code

var apiurl = "xxxxx";
function http_post(controller,data,cb){
  wx.request({
    url:apiurl+controller,
    data:data,
    method:'post',
    header:{'Content-Type':'application/x-www-form-urlencoded'},
    success:function(res){
      return typeof cb == "function" && cb(res.data)
    },
    fail:function(res){
      return typeof cb == "function" && cb(false)
    }
  })
}
module.exports = {
  http_post:http_post,//post请求
}

Front-end js uses

var wxq = require('../../utils/wxrequest.js');
var openid = wx.getStorageSync('openid');//获取缓存里面的openid

wxq.http_post('这里是控制器/方法',{'openid':openid},function(res){
    console.info('回调',res)
})

Related recommendations:

How can the mini program implement a function similar to circle of friends positioning (Amap)

How to implement the third level in the mini program Selector component? (code example)

The above is the detailed content of Code analysis of request encapsulation in WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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