Home > Article > WeChat Applet > Detailed graphic and text explanation of WeChat applet http request
This article mainly introduces the relevant information about the detailed graphic and text explanation of the HTTP request of the WeChat applet. Friends in need can refer to it.
Network communication in the WeChat applet can only communicate with the specified domain name. , WeChat applet includes four types of network requests.
Normal HTTPS request (wx.request)
Upload file (wx.uploadFile)
Download file (wx.downloadFile)
WebSocket communication (wx.connectSocket)
Set the domain name
The URL domain name is illegal, please try again after mp background configuration
You can see the setting options in the setting interface of the WeChat applet:
http request
Only 5 at the same time Network request.
function queryRequest(data){ wx.request({ url:"https://example.com/api/", data:data, header:{ // "Content-Type":"application/json" }, success:function(res){ console.log(res.data) }, fail:function(err){ console.log(err) } }) }The above code will send an http get request and then print out the returned results. The parameters are also relatively easy to understand.
String data:"xxx=xxx&xxx=xxx " in the form or Object data:{"userId":1} in the form
InterfaceSuccess callback
Upload files
function uploadFile(file,data) { wx.uploadFile({ url: 'http://example.com/upload', filePath: file, name: 'file', formData:data, success:function(res){ console.log(res.data) }, fail:function(err){ console.log(err) } }) }The url, header, success, fail and complete are the same as ordinary http requests.
The parameters that are different here are:
key corresponding to the name file. The server needs to obtain the file through the name parameter.
Download file
function downloadFile(url,typ,success){ wx.downloadFile({ url:url, type:typ, success:function(res){ if(success){ success(res.tempFilePath) } }, fail:function(err){ console.log(err) } }) }The parameters of url, header, fail, complete and wx.uploadFile are consistent. The parameters that are different are: type: The type of the downloaded resource, used for automatic identification by the client. The parameters that can be used are image/audio/video.
function svaeFile(tempFile,success){ wx.saveFile({ tempFilePath:tempFile, success:function(res){ var svaedFile=res.savedFilePath if(success){ success(svaeFile) } } }) }Use wx.saveFile to save temporary files locally and provide them for use when the applet is started next time. The parameters are:
超时的设置
在微信小程序开发:MINA中已经提到了在app.js中设置networkTimeout可以设置四种类型网络访问的超时时间:
"networkTimeout":{ "request": 10000, "connectSocket": 10000, "uploadFile": 10000, "downloadFile": 10000 }
这里设置的超时时间对应着四种类型的网络请求。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
The above is the detailed content of Detailed graphic and text explanation of WeChat applet http request. For more information, please follow other related articles on the PHP Chinese website!