1. Create the api.js file to encapsulate the Wx. Request request
//Import apilist from '. / apilist' / / import API list from '. / apilist' / / import API list from '. / apilist' / / import API list from '. / apilist' / / import API list from '. / apilist' / / import an interface list / const apirequest = (URL, method, data, The apirequest method receives the parameters required by wx.request. If it is not enough, you can add the parameters let promise = new promise (function (resolve, request) { Wx. Request ({URL: URL: host + URL, data: data: data | null, method: Method: method, header: header: header | | | | 124; {content-type-type (function (resolve, request) { wx.request request request ({URL: URL: host + URL, data: data: data: data | | null, method: Method: Method: method, header: header: header: header: header: header: header: header: Header':'application / x-www-form-urlencoded'}, Success: function (RES) {success: function (RES) {res (RES) / / the interface call is successful and resolve (RES); / / according to business needs, resolve the data of the JSON returned by the resolve interface according to business needs, and fail: function (RES) {what's more, failure: function (RES) {what's the failure of the interface call reject ({errormsg: 'network error, please try again later', code: - 1});; } } } } }, ԡ}, fail: function (RES) {failure / / interface call failure {reject ({errormsg: 'network error SG:' try again later ', code: - 1}, code: - 1} ; return promise; //Note that the return here is a promise object ԡ ԡ ԡ ԡ ԡ ԡ ԡ ԡ. index] [0], data)) }) } }; } / / using export default to export will report an unexpected token export error, so use module.exports / / export default { / / login, register / / module.exports = list; / / finally, export the object and call it to other pages
2. Create the apilist. JS file to manage the request address
//Set the IP address and port of the API interface. Let host ='http: / / 192.168.0.122:83 / ' / / list of all interfaces list let API list = {list of all interfaces let API list let apilist = login: ['post','user / Login '],, registry: ['Get','user / Register '] / / export host and apilist to export module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.module.api list.api list.list.list.exports = {host, apilist}
3. Invoking a request in a page
/ / / you need to import a package file, 𠃱ԡԡpage ({ԡ onload () {𠃱 api.login.login.http ({Username: "barken, password:" 233333333 "}), then (RES = > { console.log.log (RES); }) api.register.http (). Then (RES = = > {ԡ, HTTP (). Then (RES = > {{Republic, password:" 233333333 "}), then (RES = > {server, password:" 233333333 "}), then (RES = > { { api.register.register.http(), then (RES = > { console.log (RES); }) } })
Request effect