preface
Today, let’s take a look at the knowledge of the request data in the small program, and think about a question, is there a cross-domain problem in the small program side storage?
1. Restrictions on network data requests
For security reasons, applets have two restrictions on data interface requests:
- Only request
HTTPS
Type interface - The domain name of the interface must be added to the trust list
2. Configure Request
1. What should I pay attention to when MODIFYING the Request domain name?
Requirement description: Suppose that in your own wechat small program, you want to request the interface under the domain name https://www.escook.cn/
Configuration steps: Log in to wechat mini program management background -> Development > Development Settings -> Server domain name -> Modify the request legitimate domain name
Note the following when modifying:
- Domain name only
https
(request
,uploadFile
,downloadFile
) andwss
(connectSocket
) protocol - Domain name unavailable
IP
Address orlocalhost
- Domain name must go through
ICP
For the record - The server domain name can be changed five times in a month
2. AGET
request
Call the wx.request() method provided by wechat applet to initiate GET data request
Code examples (below) :
wx.request({
url: 'https://www.xxx.com'.// The requested interface address must be based on HTTPS
methods: 'GET'.// Request mode
data: {
// The data sent to the background
name: 'zs'.age: 22
},
success: res= > {
// The callback function after the request succeeds
console.log(res)
}
})
Copy the code
3. APOST
request
POST data request can be initiated by calling the wx.request() method provided by wechat applet
Code examples (below) :
wx.request({
url: 'https://www.xxx.com'.// The requested interface address must be based on HTTPS
methods: 'POST'.// Request mode
data: {
// The data sent to the background
name: 'ls'.gender: 'male'
},
success: res= > {
// The callback function after the request succeeds
console.log(res)
}
})
Copy the code
4. Request data as soon as the page loads
In many cases, we need to automatically request some initialized data as soon as the page loads. You need to call the function that gets the data in the onLoad event of the page
Code examples (below) :
/** * lifecycle function -- listens for page loading */
onLoad: function(options) {
this.getSwiperList()
this.getGridList()
},
// Get the data from the rotation chart
getSwiperList() {...
},
// Get the data from the nine grid
getGridList() {...
}
Copy the code
5. Skip the verification of the valid domain name of request
If the backend programmer only provides the INTERFACE of HTTP protocol, but does not provide the interface of HTTPS protocol temporarily, in order not to delay the development progress, we need to temporarily enable the option of “development environment does not verify the requested domain name, TLS version and HTTPS certificate” in the wechat developer tool. Skip server domain name verification. In this case, the server domain name will not be verified in the wechat developer tool or when the mobile phone is in debug mode.
Note: After the server domain name is configured successfully, it is recommended that developers turn off this option for development and test it on each platform to confirm that the server domain name is configured correctly.
3. On cross-domain andAJAX
instructions
-
Cross-domain problems exist only in browser-based Web development. Since the host environment of the small program is not the browser, but the wechat client, there is no cross-domain problem in the small program
-
The core of AJAX technology depends on the XMLHttpRequest object in the browser. Because the host environment of the small program is the wechat client, it is not called to initiate AJAX requests in the small program, but to initiate network data requests
conclusion
You never know your luck.