Eight, set the global program for white small function of the request and the request path

app.js:

GlobalData: { 
    the userInfo: null , 
    statusBarHeight: wx.getSystemInfoSync () [ 'statusBarHeight' ],
     the URL: 'http://www.baidu.com' --- where you want to set a global write request path 
  },

In globalData global peer write request function:

Attach Code:

wxRequest(method, url, data, callback, errFun) {
    wx.request({
      url: url,
      method: method,
      data: data,
      header: {
        'content-type': method == 'GET' ? 'application/json' : 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      dataType: 'json',
      success: function (res) {
        callback(res.data);
      },
      fail: function (err) {
        errFun(res);
      }
    })
  },
使用方法:
app.wxRequest('POST', app.globalData.URL + '/aaa', data, (res) => {
      console.log(res)
      
    }, (err) => {
      console.log(err.errMsg)
    });
 
 

 

 

Guess you like

Origin www.cnblogs.com/xintao/p/11248727.html