Home >WeChat Applet >Mini Program Development >An example of ajax data request wx.request for WeChat applet

An example of ajax data request wx.request for WeChat applet

Y2J
Y2JOriginal
2017-05-02 10:33:234112browse

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:

An example of ajax data request wx.request for WeChat applet

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!

Statement:
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