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) } })
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!