Every request sent to the background in wechat applet should write a wx.request method

wx.request({
  url: ' '.data: {},header: {
    'content-type': 'application/json'
  },
  success (res) {
  }
});
Copy the code

A small number of requests is fine, but a large number is uncomfortable, and encapsulating requests is necessary for code quality and elegance. Let’s get down to business 👉

The specific implementation

The parameters in Wx. request are basically fixed. You only need to change the value of data according to the situation, and unified processing is performed in success and FAIL

1. Create folder called HTTP in pages directory, the folder under the new three files: env. Js, index, js, request. Js

In the env. Js as follows

request.js

index.js

2. Import it in the required JS file

3. Define arrays in data and send requests where appropriate

A basic encapsulation is done as shown above, and can be added if you need to judge the status code. There is no denying that there is a more perfect treatment scheme, under the talent shallow, make a fool of 😃(spray light)