Home  >  Article  >  WeChat Applet  >  Mini program implements interactive template analysis with background data, making it easy to get started

Mini program implements interactive template analysis with background data, making it easy to get started

php是最好的语言
php是最好的语言Original
2018-08-04 10:52:534343browse

I have been working on a small program recently. It is relatively easy to design the page when I get started for the first time. But when it comes to interacting with the backend, I almost collapse. After looking at the official API, I still don’t know how to do it. I asked the seniors in the company and they directly told me I have written a template for me, let’s take a look:

/引入代码
var call = require("../util/request.js")

Page({
  data: {
    pictureList: [],
  },

  onLoad: function () {
    var that = this;
    //调用封装的方法,为了方便我直接在页面加载的时候执行这个方法
    call.getData(url, this.shuffleSuc, this.fail);
    this.loadMsgData(that);
  },
  shuffleSuc: function (data) {
    var that = this;
    that.setData({
      pictureList: data.rows
    })
    //我后面测试了一下,直接this.setData也可以,但是因为我在没有使用封装方法的时候
    //this.setData报过错,不能直接用this,所以我在赋值的时候一般都会加上var that = this;
  },
  fail: function () {
    console.log("失败")
  },
})

The front-end and back-end interaction is like this:

1. Under the condition that there is no need to pass a value: the background method uses GET

Mini program implements interactive template analysis with background data, making it easy to get started

This place needs to be changed to get, and then it can be written according to the template above

2. Some values ​​need to be passed: use POST in the background

Mini program implements interactive template analysis with background data, making it easy to get started

Then, change the template

call.request('corresponding background method', {value to be passed}, this.success, this.fail) here You need to give getData as a request, and then write the value in it and pass it to the backend. At the same time, the backend must have corresponding accepted variables;

For example: Mini program implements interactive template analysis with background data, making it easy to get started

I want to put the theme and The type and content are passed to the background, and at the same time it is necessary to know who wrote it (using openid), then the corresponding call is: call.request('method', {openid: the openid to be worn, title: e.detail.value( Get the value of input), type: Same reason, content: Same reason}, this.success, this, fail), corresponding to the background, I use a separate variable to receive openid, and the others use an object to receive it, so this is achieved The applet passed the value to the background;

The interaction between the applet and the background was realized in this way. Oops, I finally finished receiving all the data.

Related articles:

WeChat applet wx.request realizes background data interaction function analysis

WeChat applet realizes interaction with background PHP

Related videos:

WeChat Mini Program Event Interaction-Qianfeng Education WeChat Mini Program Development Video Tutorial

The above is the detailed content of Mini program implements interactive template analysis with background data, making it easy to get started. 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