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:
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!