WeChat applet HTTP interface request package code instance

1. Method package (new folder Util, tool file, create a request.js file under the folder, used to pack the method package)

Request.js:

 VAR App = getApp (); // Project URL The same part, mitigate the amount of code, and convenient for project migration / / here because I am local debugging, so Host is not standardized, it should actually be Domain Name Information Var Host = 'http:// localhost: 8081 / divo /'; / ** * POST request, * URL: interface * PostData: parameter, JSON type * dosuccess: successful callback function * DOFAIL: failed Tune function * / function request (URL, PostData, Dosuccess, Dofail) {wx.request (true interface of {// project, implement URL: Host + URL, Header: {"Content-Type": {"Content-Type" by string splicing: "Application / JSON; Charset = UTF-8"}, data: postdata, method: 'post', success: function (res) {// parameter value is RES.DATA, directly returned data to DOSUCCCESS (RES. Data);}, fail: function () {DOFAIL ();},}} // GET request, do not need to pass, direct URL call, Function GetData (URL, DOSUCCESS, DOFAIL) {wx.request ({URL : Host + URL, Header: {"ContenT-TYPE ":" Application / JSON; Charset = UTF-8 "}, method: 'get', Success: function (res. rd);}, fail: function () {DOFAIL (); },})} / ** * Module.Exports to export code * JS files through var call = required ("../ util / request.js") loading * Introduction "" inside " Through ../../../ This type, the compiler of the applet will automatically prompt, because you may * project directory more than one level, different JS files correspond to the location of the tool class * / module.exports .Request = Request; module.exports.getdata = getdata;   
2.Page Create a folder, create four files, joining in JS

// Introduced Code Var Call = Require (“../ Util / Request.js”) Page ({Data: {PictureList: [],}, ONLOAD: FUNCTION ) {var there = this; // Call the package method, in order to facilitate me to execute this method directly on the page CALL.GETDATA (‘Lunbo.Do’, this.shufflesuc, this.fail); // is useless, front Forgot your comment, sorry // this.loadmsgdata (That);}, shufflesuc: Function (DaTA) {var this = this; That.SetData ({PictureList: Data.Rows}) // I tested it later, directly this.setdata can also, but because I didn’t use the package method, //this.setdata If you have an error, you can’t use this, so I usually add VAR THAT = this; // this sentence is not a habit}, fail: function () {Console.log (“failed) },})
 Write the callback function in the PAGE, which is called by this mode when calling the package method, so that the That.SetData method is valid. If it is written outside the Function method, the compiler will not report error, but in wxml is invalid, I have not been studying, and at the time of calling in the call.getdata method, although it can enter the method, it is invalid. So I don't recommend this way:   
Function shufflesuc (data) {var there = this; try.setdata ({PictureList: Data.Rows})

3. After running, the value of the DATA can be printed by the console appdata through the applet, and can also be displayed in the page as needed.
is this All content, I hope to help everyone, I hope everyone will support Tumi Clouds.
                     
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment