Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to encapsulate request Interface in WeChat Mini Programs

Shulou Source: shulou.com Published: 2022-05-31 13:35:40 09月24日 Update

The knowledge of this article "how to encapsulate request interface in WeChat Mini Programs" is not quite understood by most people, so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to encapsulate request interface in WeChat Mini Programs" article.

The encapsulation example code of WeChat Mini Programs's request interface

Encapsulation of Mini Program request API (essentially a callback to the request callback function)

Module.exports.getData = function (url) {var data = arguments.length > 1 & & arguments [1]! = undefined? Arguments [1]: {}; var method = arguments.length > 2 & & arguments [2]! = undefined? Arguments [2]: 'POST'; var header = arguments.length > 3 & & arguments [3]! = undefined? Arguments [3]: {'Content-Type':' application/x-www-form-urlencoded; charset=UTF-8'}; return new Promise (function (resolve, reject) {wx.request ({url: url, data: data, method: method, header: {'Content-Type':' application/x-www-form-urlencoded Charset=UTF-8'}, success: function (res) {resolve (res)}, fail: function (res) {reject (res)}})})}

Call method

Var common = require (".. /.. / common/common.js") common.getData (postUrl). Then (function (data) {this.setData ({/ /.... }); console.log (data);}). The above article is about "how to encapsulate the request interface in WeChat Mini Programs". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.

Tags: Interfaces programs encapsulation content articles knowledge articles code value again functions most examples that is methods more essence steps knowledge points industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Xiaomi MySQL Huawei Apple