Home  >  Article  >  WeChat Applet  >  WeChat Mini Program Development Guide: About Network Requests

WeChat Mini Program Development Guide: About Network Requests

高洛峰
高洛峰Original
2017-03-04 14:36:171736browse

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:

WeChat Mini Program Development Guide: About Network Requests

Set the domain name
Needs to be in the mini program of the WeChat public platform Set the domain name. You can see the setting options in the setting interface of the WeChat applet:

WeChat Mini Program Development Guide: About Network Requests

Select development settings:

WeChat Mini Program Development Guide: About Network Requests

You can see the server settings:

WeChat Mini Program Development Guide: About Network Requests

#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 officially develop small programs, you still need an https domain name, but for learning, http is enough.

Network requests in mini programs 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:

Using wx.request, you can 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
· The data request parameter can be in the form of String data: "xxx=xxx&xxx=xxx" or Object data: {"userId":1}
· header Set the header of the request
· method http method, the default is GET request
· success callback for successful interface
· fail callback for failed interface

There is also another parameter that is not included In the code:
· complete is the callback after calling the interface. Regardless of success or failure, the interface will be called.

Timeout setting

has been mentioned in the previous article in app.js Setting networkTimeout can set the timeout for four types of network access:

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

For more WeChat applet development guides: For articles related to network requests, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn