Small micro-channel use callback procedures wx.request

The micro-channel external js applet to call wx.request方法upon, because the mechanism for asynchronous requests, we can not on its success:function()return in the required data directly.

example:

One:

// This method is an external file "utils / util.js" be defined in 
function REQUEST_METHOD (URL, the callback) 
{ 
  wx.request ({ 
    URL: URL, 
    Method: 'the GET' , 
    header: {
       'the Type-the Content': 'file application / JSON' 
    }, 
    Success: function (RES) { 
      the callback && the callback (res.data); 
    } 
  }); 
} 

// need to add to expose this method you define, or can not be found outside the
module. = {Exports
REQUEST_METHOD: REQUEST_METHOD
}
 

Two corresponding to the current page js method:

// first into the public JS 
var util = the require ( '../../ utils / util.js' ); 

util.request_method ( URL, (RES) => {
       the this .setData ({ 
        otherData: RES 
      }); 
});

 

Guess you like

Origin www.cnblogs.com/z-hj/p/12409040.html