Home >WeChat Applet >Mini Program Development >Where is the WeChat applet calling interface written?

Where is the WeChat applet calling interface written?

angryTom
angryTomOriginal
2020-03-24 13:41:119943browse

Where is the WeChat applet calling interface written?

Where to write the WeChat applet calling interface

The WeChat applet calling interface can be written in the life cycle callback function of the applet, such as onLaunch, onShow, onHide, etc. can also be written in custom functions, such as custom button click events viewTap, etc.

Recommended learning: Small program development

Where is the WeChat applet calling interface written?

1. Write the calling interface code in the life cycle of App() In the callback function

App({
  onLaunch (options) {
      // 调用接口,发送网络请求
      wx.request({
      url: 'test.php', //仅为示例,并非真实的接口地址
      data: {
        x: '',
        y: ''
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data)
      }})
  }
)

2. Write the interface calling code in the custom function

//index.js
Page({
  // 自定义函数
  viewTap: function() {
    // 调用接口,发送网络请求
      wx.request({
      url: 'test.php', //仅为示例,并非真实的接口地址
      data: {
        txt: '123'
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data)
      }})
  }
})

PHP Chinese website, a large number oflaravel tutorials, welcome to learn!

The above is the detailed content of Where is the WeChat applet calling interface written?. 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