Home > WeChat Applet > Mini Program Development > An example of how WeChat applet API calls wx.request to implement data requests

An example of how WeChat applet API calls wx.request to implement data requests

php是最好的语言
Release: 2018-08-07 09:49:52
Original
7152 people have browsed it

An example of how WeChat applet API calls wx.request to implement data requests

Preface

##How does the WeChat applet call API to implement data request-wx.request()? The 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, a 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)
  }})
Copy after login

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. Then, copy the link to the URL in the template.


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

An example of how WeChat applet API calls wx.request to implement data requests

Click to create an interface.

An example of how WeChat applet API calls wx.request to implement data requests

Update the data and create your own data.

An example of how WeChat applet API calls wx.request to implement data requests

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

An example of how WeChat applet API calls wx.request to implement data requests

An example of how WeChat applet API calls wx.request to implement data requests

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>
Copy after login

在接着js文件中添加事件。

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

点击按钮如果出现下面的错误,那是因为要到详情按钮中,给个地方打个勾即可。

An example of how WeChat applet API calls wx.request to implement data requests

找到下方打钩即可。

An example of how WeChat applet API calls wx.request to implement data requests

点击按钮,即可查看,请求数据效果出现了。

An example of how WeChat applet API calls wx.request to implement data requests

结语

  • 你完成了吗?是不是,很容易呢?是不是很简单呢?

An example of how WeChat applet API calls wx.request to implement data requests

相关推荐:

微信小程序的ajax数据请求wx.request的实例介绍

怎么玩转微信小程序的ajax数据请求

The above is the detailed content of An example of how WeChat applet API calls wx.request to implement data requests. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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