封装代码
const HTTP_BASE_URL = "https://baidun.com/";
function api(_methods,url,data,callback){
wx.request({
url: HTTP_BASE_URL+url,
method: _methods,
data: data,
dataType: 'json',
success: (res)=>{
typeof callback == "function" && callback(res, "");
},
fail: (res)=>{
console.log('请求数据失败')
console.log(err)
typeof callback == "function" && callback(err, "");
}
});
}
接收方式
export function getJSON(url,data,callback){
api('GET',url,data,callback)
}
export function postJSON(url,data,callback){
api('POST',url,data,callback)
}
局部引用
import { getJSON, postJSON } from '../../utils/server.js';
使用封装
postJSON('wuxingHistory',{user_id:app.globalData.userId}, res => {
if(res.data.code==0){
let data = res.data.result
this.setData({
recordList: data
})
}
})