Many students cannot find the location of the ajax data request of the WeChat applet. Here we list the ajax requests of the applet separately. The request of the WeChat applet is the wx.request API, wx.request (some Object parameters), WeChat applet is different from the browser's ajax request, and can directly make cross-domain requests without considering cross-domain issues.
Use the data request API officially provided by the mini program to initiate a data request
wx.request(OBJECT)
wx.request initiates an https request. A WeChat applet can only have 5 network request connections at the same time.
OBJECT parameter description:
parameter name | type | Required | illustrate |
---|---|---|---|
url | String | yes | Developer server interface address |
data | Object、String | no | Requested parameters |
header | Object | no | Set the header of the request. Referer |
cannot be set in the header. method | String | no | The default is GET, valid values: OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT |
success | Function | no | Receive the callback function returned successfully by the developer service, res = {data: 'Content returned by the developer server'} |
Function | no | Callback function for interface call failure | |
Function | no | The callback function at the end of the interface call (will be executed if the call is successful or failed) |
wx.request({ url: 'test.php', data: { x: '' , y: '' }, header: { 'Content-Type': 'application/json' }, success: function(res) { console.log(res.data) } })Using fetch to make ajax requests in the WeChat appletFetch is a new ajax request specification. Fetch is also supported in small programs. The test ajax request code is as follows:
Then the code in the middle is a test. Here is an excerpt of a small part of the code for actual use. Modify yourself.
fetch('http://www.php.cn/json.php?typeid=34&page=1&pagesize=10') .then(function(response){ if(response.status==200){ that.data.page++; return response.json(); } }).then(function(data){ console.log(data); //更新数据 that.setData({ listArr:that.data.page==1 ? data : that.data.listArr.concat(data) }) console.log(that.data.listArr); })
The above is the detailed content of How to play with ajax data request of WeChat applet. 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

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
