Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement http request encapsulation in WeChat Mini Programs

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to achieve http request encapsulation in WeChat Mini Programs". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

WeChat Mini Programs http request encapsulation

Sample code

Wx.request ({url: 'test.php', / / is only an example, not the real interface address 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 Mini Program. If it is tedious to write like this each time in the actual code, then let's do the encapsulation.

What are we more concerned about in the code?

1. Parameter of request, interface to be accessed

2.GET/POST... Request mode

3. Unified processing of request parameters (for example: encryption, setting common parameters.)

4. The data returned successfully after the request (for example, decryption, extraction of logic data)

5. Request failure feedback

What don't we focus on?

1. Request url (usually the fixed configuration is somewhere)

two。 Make different request parameters according to different interface rules (for example, 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 request function GET (requestHandler) {request (' GET',requestHandler)} / / POST request function POST (requestHandler) {request ('POST',requestHandler)} function request (method) RequestHandler) {/ / Note: you can deal with params encryption, var params = requestHandler.params, etc. Wx.request ({url: API_URL, data: params, method: method, / / OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT / / header: {}, / / set header success: function (res) {/ / Note: requestHandler.success (res)}, fail: function () {requestHandler.fail ()} Complete: function () {/ / complete}})} module.exports = {GET: GET, POST: POST}

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

/ / Import js var network = require (".. /.. / utils/network.js") / / write parameter var params = new Object () params.api_name = "api_user_login" params.account = "hanqing" params.password = "123456" / / initiate a request network.GET ({params: params, success: function (res) {console.log (res) / / get the decrypted data Code logic}, fail: function () {/ / logic after failure},}) this is the content of "how to implement http request encapsulation in WeChat Mini Programs". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report