Home > WeChat Applet > Mini Program Development > How to play with ajax data request of WeChat applet

How to play with ajax data request of WeChat applet

伊谢尔伦
Release: 2017-05-30 09:36:40
Original
3525 people have browsed it

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:

##               fail           Function             no Callback function for interface call failure complete             Function             no The callback function at the end of the interface call (will be executed if the call is successful or failed)
            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'}
Sample code:

wx.request({
  url: 'test.php',
  data: {
     x: '' ,
     y: ''
  },
  header: {
      'Content-Type': 'application/json'
  },
  success: function(res) {
    console.log(res.data)
  }
})
Copy after login

Using fetch to make ajax requests in the WeChat applet

Fetch 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);
    })
Copy after login


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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template