This article mainly introduces the detailed description of network requests for WeChat mini programs. The editor thinks it’s pretty good, so I’d like to share it with you now and give it as a reference. Let’s follow the editor and take a look.
When we talked about configuration in the previous article, we said that when developing small programs, you can choose to have APPID or without APPID. There are two ways.
1. When there is an APPID, network communication can only communicate with the specified domain name. If there is no configuration, the following error will be reported during compilation:
Configuration method:
Set the domain name
You need to set the domain name in the mini program of the WeChat public platform. You can see the setting options in the setting interface of the WeChat applet:
Select development settings:
You can see the server settings:
#Here we can set the domain names that our APPID can access, and we can set up to two of each type. (Note that only https domain names can be used here. This application process takes a certain amount of time)
2. When there is no APPID, it is much more convenient. You can make network requests at will without limiting the domain name. , however, publishing or previewing on mobile phones is not possible in this case. If you want to formally develop small programs, you still need an https domain name, but http is enough for learning.
In the mini program, network requests are roughly divided into four types.
Ordinary HTTPS request (wx.request)
Upload file (wx.uploadFile)
Download file (wx.downloadFile)
WebSocket communication (wx.connectSocket)
Here we mainly talk about wx.request:
Use wx.request to initiate an http request. A WeChat applet is limited to only 5 network requests at the same time. Note that it is 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 will send an http get request, and the parameters are relatively easy to understand.
url The url address of the server
data The requested parameters can be String data:”xxx=xxx&xxx=xxx ” form or Object data:{“userId”:1} form
header Set the request header
Method http method, the default is GET request
success InterfaceSuccess callback
fail Interface failure callback
There is another parameter that is not in the code:
complete is the callback after calling the interface. Regardless of success or failure, the interface will be Call
Timeout setting
As mentioned in the previous article, setting networkTimeout in app.js can set the timeout for four types of network access. Time:
"networkTimeout":{ "request": 10000, "connectSocket": 10000, "uploadFile": 10000, "downloadFile": 10000 }
The above is the detailed content of WeChat Mini Program Network Request Detailed Description. For more information, please follow other related articles on the PHP Chinese website!