


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.
- url The url address of the server
- data The requested parameters can be
String data:"xxx=xxx&xxx=xxx " in the form or Object data:{"userId":1} in the form
- header Set the request header
- success
InterfaceSuccess callback
- fail Interface failure 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:
- tempFilePath The path of the file that needs to be saved
- success Callback for successful saving, returns the path of successful saving, use res.savedFilePath to obtain the saved file Success path
- fail failure callback
- complete end callback
超时的设置
在微信小程序开发: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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
