• Welcome to the eight year blog, I'm glad to meet you at the right age!
  • Due to the theme, QQ login partners will display the default avatar in the comments. Please go to the personal center and upload the avatar again.

Wechat applet encapsulates request request

Code Notes barben 4 months ago (04-22) 157 views 0 comments

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

Now that you're done, you can use your encapsulated request to replace the applet's request.

Although it can achieve the desired effect perfectly, the writing method of this wheel is not very crisp. A section of for loop is used in the code, which is considered to be the biggest failure. Welcome your advice!


Eight blogs that year, we've been there all the time
If the author does not indicate the original article, please indicate the link and source of this article
Wechat applet encapsulates request request - https://www.barben.cn/code/800.html
Like it( two )
Post my comments
Cancel comment
expression Mapping Bold Strikethrough Center Italics

You need to bring your nickname and email with you in the year of eight!

  • Nickname (required)
  • Email address (required)
  • website