The applet encapsulates the http access network library instance code, and the applet instance
Small Program encapsulate the code for accessing the network library through http
Previously, LeanCloud was used for storage. Now, the following encapsulation is used for calling traditional APIs:
Document Source: https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html
The Code is as follows:
Var HOST = 'HTTP: // localhost/lendoo/public/index. php/'; // The website request interface, unified as postfunction post (req) {// initiate a network request wx. request ({url: HOST + req. uri, data: req. param, header: {"content-type": "application/x-www-form-urlencoded"}, method: 'post', success: function (res) {req. success (res. data)}, fail: function (res) {console. log (res) ;}} // export module. exports = {post: post}
Then the frontend call can be done as follows:
var http = require('../../utils/http.js');... http.post({ uri: http.orderListUri, param: { third_session: wx.getStorageSync('third_session') }, success: function (data) { that.setData({ orderList: data }); } });
Generally, when you use an interface written by yourself, the method or header is agreed, so you do not need to write it again.
header: { "content-type": "application/x-www-form-urlencoded" },method: 'POST'
The fail callback method can also be processed in a unified manner. Further, the code value in the success callback can be further determined, and specific error codes can be processed in a unified manner, such as redirecting to the logon page.
After the above processing, is it concise?
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!