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
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
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:
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:
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!