search
HomeWeChat AppletMini Program DevelopmentDetailed explanation of http request encapsulation of WeChat applet

WeChat applet httpRequest encapsulation

Sample code

wx.request({
 url: 'test.php', //仅为示例,并非真实的接口地址
 data: {
   x: '' ,
   y: ''
 },
 method:'POST',
 header: {
   'content-type': 'application/json'
 },
 success: function(res) {
  console.log(res.data)
 },
 fail: function( res ) {
   fail( res );
  }
})

The above is the basic http request code of the applet. In the actual code, if it is written like this every time, It's more complicated, so let's do some encapsulation.

What do we pay more attention to in the code?

1. Request parameters, access interface
2.GET/POST...Request method
3.Request parametersUnified processing (for example: encryption, setting public Parameters...)
4. Data returned after successful request (for example: decryption, extraction of logical layer data)
5. Feedback on failed request

What do we not pay attention to?

1. Request url (generally fixed configuration somewhere)
2. Make different request parameters according to different interface rules (such as parameter encryption, etc.)
...

Let’s practice the code

network.js

var API_URL = 'http://localhost/loverule/api/api.php'
 
var requestHandler = {
  params:{},
  success: function(res){
    // success
  },
  fail: function() {
    // fail
  },
}
 
//GET请求
function GET(requestHandler) {
  request('GET',requestHandler)
}
//POST请求
function POST(requestHandler) {
  request('POST',requestHandler)
}
 
function request(method,requestHandler) {
  //注意:可以对params加密等处理
  var params = requestHandler.params;
 
  wx.request({
   url: API_URL,
   data: params,
   method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
   // header: {}, // 设置请求的 header
   success: function(res){
    //注意:可以对参数解密等处理
    requestHandler.success(res)
   },
   fail: function() {
    requestHandler.fail()
   },
   complete: function() {
    // complete
   }
  })
}
 
module.exports = {
 GET: GET,
 POST: POST
}

Call in the page (take GET request as an example)

//Import js

var network = require("../../utils/network.js")
 
//写入参数
var params = new Object()
params.api_name = "api_user_login"
params.account = "hanqing"
params.password = "123456"
 
//发起请求
network.GET(
{
  params: params,
  success: function (res) {
   console.log(res)
   //拿到解密后的数据,进行代码逻辑
 
  },
  fail: function () {
   //失败后的逻辑
 
  },
})

The above is the detailed content of Detailed explanation of http request encapsulation of 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)