WeChat applet network request details

Source: Internet
Author: User
This article describes in detail the network requests of applets. I think it is quite good. now I will share it with you and give you a reference. Let's take a look at the small series. This article mainly introduces the detailed description of small program network requests. I think it is quite good. now I will share it with you and give you a reference. Let's take a look at it with Xiaobian.

As we mentioned in the configuration above, you can choose either APPID or APPID when developing a applet.

1. When an APPID is available, network communication can only be performed with the specified domain name. If no configuration is made, the following error will be reported during compilation:

Here we can set the domain name that our APPID can access, and each type can be set up to two. (Note: only https domain names can be used here. this application takes some time)

2. if you do not have an APPID, it is much more convenient. you can make network requests without limiting domain names. However, in this case, you cannot publish or preview it on your mobile phone. If you want to officially develop small programs, you still need an https domain name. However, http is sufficient when learning.

Network requests in applets are roughly divided into four types.

  • Common HTTPS request (wx. request)

  • Upload a file (wx. uploadFile)

  • Download File (wx. downloadFile)

  • WebSocket communication (wx. connectSocket)

Wx. request:

You can use wx. request to initiate an http request. a applet is limited to only five network requests at the same time. Note that at the same time.

    wx.request({      url: 'http://192.168.1.137:80/app/guanggao',      method: 'POST',      data: {         type: "1"      },      header: {        'Accept': 'application/json'      },      success: function (res) {        that.setData({          images: res.data.data.guanggao        })      }      fail:function(err){        console.log(err)      }    })

The above code sends an http get request, and the parameters are easy to understand.

  • Url server url

  • Data request parameters can be in the format of String data: "xxx = xxx & xxx = xxx" or Object data: {"userId": 1 }.

  • Header: Set the request header.

  • Method http method. the default value is GET request.

  • Successful callback of success interface

  • Callback for failed fail interface

Another parameter is not included in the code:

  • After the complete API is called, the API is called no matter whether it is successful or fails.

Timeout settings

Previously, we mentioned that you can set four types of network access timeout when setting networkTimeout in app. js:


"networkTimeout":{  "request": 10000,  "connectSocket": 10000,  "uploadFile": 10000,  "downloadFile": 10000}

The above is a detailed description of the small program network request. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.