[wechat applet] network data request

Time:2022-6-2

1. restrictions on network requests in applets

For security reasons, the applet official has made the following two restrictions on the request for data interface:

  1. Use onlyhttpsInterface of type
  2. MustDomain name of the interfaceAdd toTrust listin

image

2. configure the request legal domain name

image

be careful:

  1. Domain name only supports HTTPS protocol
  2. Domain name cannot use IP address or localhost
  3. Domain name must be filed by ICP
  4. The domain name server can apply for up to 5 modifications a month

3. initiate a get request

Call wx The request () method can initiate a get data request. The example code is as follows:

image

4. initiate post request

Call wx The request () method can initiate a post data request. The example code is as follows:

image

5. send the request when the page is just loaded

In many cases, we need toWhen the page is just loaded, it automatically requests some initialization data。 In this case, theonLoadCall the function to get data in the event. The example code is as follows:

image

6. skip request legal domain name verification

If the back-end programmerOnly the interface of HTTP protocol is providedTemporaryNo HTTPS protocol providedInterface for.

At this time, in order not to delay the development progress, we can temporarily open theThe development environment does not verify the requested domain name, TLS version and HTTPS certificate“Option to skip the verification of the request legal domain name.

image

Note *: the option to skip the verification of the request legal domain name is only available in the development and debugging phase

7. notes on cross domain and ajax

Cross domain problems only exist in browser based web development. Since the hosting environment of the applet is not a browser, but a wechat client, there is no cross domain problem in the applet.

The core of Ajax technology depends on the XMLHttpRequest object in the browser. Since the hosting environment of the applet is a wechat client, the applet cannot be called “initiating Ajax request”, but “initiating network data request”.

8. the parameter in the data object cannot be received by the server when sending the request

Set request header

wx.request({
          url: 'http://112.74.113.139:8889/api/goods/login',
          method:"post",
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data:{
              phone:13279505476,
              password:"1143220150"
          },
          success:(res)=>{console.log(res)},
        })

9. wechat applet requests HTTP data

I found the Wx of wechat applet when learning applet some time ago The request could not request the HTTP protocol header. I checked a lot from the Internet. I feel that the following is very useful. I posted it for everyone to see. Learn from each other and help each other. Don’t spray. Thank you.

  1. First, create a new cloud function
  2. Then install a plug-in
    NPM install got // install the
  3. Declaration after installation

image

  1. The body that writes code in a cloud function
    exports.main = async (event, context) => {
    
    return await rp(API)
      .then(res => {
        return res;
        })
      .catch(res => {
        return res;
        })
    }

After completion, you can request the HTTP protocol. When using it, you can call the corresponding interface of the applet

image

Recommended Today

A front-end developer's Vim is the same as an IDE

Here is my new configurationjaywcjlove/vim-webI've been grinding it, and it's basically ready to use. Take it out and cheat the star Install The latest version of Vim 7.4+ uses (brew install macvim) installation, vim version updatebrew install macvim –override-system-vim View configuration locations # Enter vim and enter the following characters :echo $MYVIMRC download vim-web Download […]