preface

Wechat applet does not have Ajax, so how does it achieve the function of data request? Call wx. Request (OBJECT) API is provided in wechat, which is very good. Here’s how to request data, which is incredibly simple.

wx.request

When looking at the documentation, sample templates are provided as follows:

wx.request({
  url: 'test.php'// This is an example, not a real interface address data: {x:' ' ,
     y: ' '
  },
  header: {
  	'content-type': 'application/json'// Default value}, success:function(res) {
    console.log(res.data)
  }
})
Copy the code

How to fetch the data is a challenge, but it is possible to simulate the call. Because there is a website, easy-mock.com/, that provides a request for simulated data…


Log in to the official website as shown in the picture, log in, and then click the arrow to enter the next page.

Click Create Interface.

Update the data and create your own data.

A preview effect, copy the link into the URL template.

Then we open the development tool of wechat applet and add the button request in the interface.

 <button type='primary' bindtap='send'</button>Copy the code

Then add the event to the JS file.

//send
  send: function(){
    wx.request({
      url: 'Copied link'// This is an example, not a real interface address data: {x:' ',
        y: ' '
      },
      header: {
        'content-type': 'application/json'// Default value}, success:function (res) {
        console.log(res.data)
      }
    })
  }
Copy the code

Click the button if the following error, that is because to the details of the button, to place a tick.

Find the bottom check.

Click the button, you can see, request data effect appears.

conclusion

  • Have you finished? Isn’t it easy? Is it easy?

  • A small gift to walk or praise