app.js:
globalData: { userInfo: null, statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'], URL: 'http://www.baidu.com' ---这里写你要设置的全局请求路径 },
在globalData同级写全局请求函数:
附上代码:
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) });