How to encapsulate request Interface in WeChat Mini Programs
Today, the editor will share with you the relevant knowledge points about how to encapsulate the request interface in WeChat Mini Programs. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
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);}); these are all the contents of this article entitled "how to encapsulate the request Interface in WeChat Mini Programs". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.