Small micro-channel request HTTP interface program package

1. The method of the package (util new folder, the tool file, create request.js file folder, a method for packaging)
request.js:

var app = getApp (); 
the same part of the project // URL, reduce the amount of code, and easy migration project 
// here because I am a local debugging, the host is not standardized, in fact, should be your domain name registration information 
var host = 'http : // localhost: 8081 / Demo / '; 

/ ** 
 * the POST request, 
 * the URL: Interface 
 * postData: parameter, json type 
 * doSuccess: successful callback 
 * doFail: failed callback function 
 * / 
function request (URL, postData, doSuccess, doFail) { 
  wx.request ({ 
    true // program interfaces, implemented by the string splicing 
    URL: Host + URL, 
    header: { 
      "Content-type": "file application / JSON; charset = UTF-. 8 " 
    }, 
    data: postData, 
    Method: 'the POST', 
    Success: function (RES) { 
      // parameter value res.data, passed directly to the returned data 
      doSuccess (res.data); 
    },
    Fail: function () { 
      doFail (); 
    }, 
  }) 
} 

// the GET request, parameter passing without direct URL call, 
function the getData (URL, doSuccess, doFail) { 
  wx.request ({ 
    URL: Host + URL, 
    header: { 
      "Content-type": "file application / JSON; charset = UTF-. 8" 
    }, 
    Method: 'the GET', 
    Success: function (RES) { 
      doSuccess (res.data); 
    }, 
    Fail: function () { 
      doFail (); 
    }, 
  }) 
} 

/ ** 
 * module.exports used to derive the code 
 * js file require ( "../ util / request.js" ) is loaded by Call = var 
 * file when introduced into the introduction " "there's this type of content through ../../../, applet compiler will automatically prompt, because you might 
 * project more than one directory, different js file corresponding to the location of the tools is not the same 
 * / 
module.exports.request = Request; 
module.exports.getData = getData;

2.page inside just create a folder, create four files, which added js 

// Code introduced 
var = Call the require ( "../ util / request.js") 

Page ({ 
  Data: { 
    pictureList: [], 
  }, 
  
  the onLoad: function () { 
    var = that the this; 
    // call the encapsulation method in order to facilitate the implementation of this method directly at me when the page loads 
    call.getData ( 'lunbo.do', this.shuffleSuc, this.fail); 
    // useless, forget the previous comment, sorry 
    // this.loadMsgData (that); 
  }, 
  shuffleSuc: function (Data) { 
    var = that the this; 
    that.setData ({ 
      pictureList: data.rows 
    }) 
    // I later tested that is directly this.setData may be, but because I do not use a method for packaging when 
    //this.setData newspaper at fault, you can not use this, so I tend to add var that = this directly at the time of assignment; 
    // this sentence is not regarded as a customary habit 
  },  
  Fail: function () {
    console.log ( "failed") 
  } 
})

The callback function written in the inside page, method name to call when calling encapsulation method by this, so that we can ensure that.setData method is effective, if it is written on the outside of the function method applet compiler does not complain, but wxml in bindtap invalid, I did not get to the bottom, and when called in call.getData method even though it can enter the process, but the assignment is invalid, so I do not recommend this way:

function shuffleSuc(data) {
  var that = this;
  that.setData({
    pictureList: data.rows
  })
}

3. After the run value of the data can be printed inside the console AppData applet can also be shown on the page as needed

 

Guess you like

Origin www.cnblogs.com/wuliujun521/p/11458379.html