Home > Article > WeChat Applet > An example of ajax data request wx.request for WeChat applet
Introduction to the ajax data request wx.request of the WeChat applet. 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 requests of the WeChat applet It 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 provided by the mini program official
to initiate a data request method
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:
Sample code:
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 applet
Fetch is a new ajax request specification. After testing by lazy website builders, 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. The actual You need to modify it yourself for use.
fetch('http://www.51xuediannao.com/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 An example of ajax data request wx.request for WeChat applet. For more information, please follow other related articles on the PHP Chinese website!