Home >WeChat Applet >Mini Program Development >Implementation of interaction between mini program and background (with code)

Implementation of interaction between mini program and background (with code)

不言
不言Original
2018-08-10 15:34:598445browse

The content of this article is about the implementation of the interaction between the mini program and the background (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

I have been working on a small program recently. When I started it for the first time, it was relatively easy to design the page. But when I was designing to interact with the background, I almost collapsed. After looking at the official API, I still can’t figure it out. Ask The seniors of the company wrote the template directly 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 values: the back-end method Use GET

This place needs to be changed to get, and then just write according to the above template

2. You need to pass some values: use POST# in the background

##Then, change the template

call.request('corresponding background method', {value to be passed}, this.success,this. fail) Here we 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:

I want to put the theme, type and content Pass it to the background, and also need to know who wrote it (using openid), then the corresponding call is: call.request('method',{openid: the openid to be passed, title: e.detail.value(get input input value), type: Same reason, content: Same reason}, this.success, this, fail), corresponding to the background, I use a separate variable to receive openid, and use an object to receive the others, thus realizing the mini program Pass the value to the background;

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

Related recommendations:

Analysis of life cycle in mini program (with code)

WeChat mini program example: how to implement marquee Animation effect (with code)

Complete code for automatic loading of mini program

The above is the detailed content of Implementation of interaction between mini program and background (with code). 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