Home  >  Article  >  WeChat Applet  >  How to call API to implement data request in WeChat applet

How to call API to implement data request in WeChat applet

不言
不言Original
2018-08-10 17:03:369275browse

The content of this article is about how to call the API to implement data requests in the WeChat applet. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

WeChat applet does not existajax, so how does it implement the data request function ? WeChat provides the API callwx.request(OBJECT), this is very good. Let’s talk about how to request data. It’s so simple.

wx.request

##When looking at the document, the sample template is provided as follows:

wx.request({
  url: 'test.php', //仅为示例,并非真实的接口地址
  data: {
     x: '' ,
     y: ''
  },
  header: {    'content-type': 'application/json' // 默认值
  },
  success: function(res) {
    console.log(res.data)
  }})

How to retrieve data is a difficult problem, but it is possible to simulate the call. Because there is a URL: https://easy-mock.com/ , which provides simulation data request. So let’s simulate the data first. When the time comes, copy the link to the URL in the template.

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

How to call API to implement data request in WeChat applet

Click to create an interface.

How to call API to implement data request in WeChat applet

Update the data and create your own data.

How to call API to implement data request in WeChat applet

You can preview the effect and copy the link to urlIn the template.

How to call API to implement data request in WeChat applet

How to call API to implement data request in WeChat applet

Then we open the WeChat applet development tool and add it to the interface Just press the button to request it.

 <button type=&#39;primary&#39; bindtap=&#39;send&#39;>请求数据</button>

Add events in the next js file.

//send
  send: function(){
    wx.request({
      url: &#39;复制的链接&#39;, //仅为示例,并非真实的接口地址
      data: {
        x: &#39;&#39;,
        y: &#39;&#39;
      },
      header: {
        &#39;content-type&#39;: &#39;application/json&#39; // 默认值
      },
      success: function (res) {
        console.log(res.data)
      }
    })
  }

If the following error occurs when clicking the button, it is because you need to go to the details button and just tick a place.

How to call API to implement data request in WeChat applet

Find the check box below.

How to call API to implement data request in WeChat applet

Click the button to view the request data effect.

How to call API to implement data request in WeChat applet

Related recommendations:

Methods for WeChat applet to pass parameters and receive data

Parsing of page compatible h5 tags in mini programs

The above is the detailed content of How to call API to implement data request in 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