vue axios请求get,post,postJson正确写法
axios请求的写法,摸索了半天,差一点都会不成功。
function get(that,url,params,successCallback,errorCallback){
axios.get(url,{
params:params
}).then(
function(response){
console.log('success:', response.data);
successCallback(response.data);
}
).catch(
function(error){
console.log("error:",error.response.data);
errorCallback(error.response.data);
}
);
}
function post(that,url,params,successCallback,errorCallback){
axios({
url:url,
method:"post",
headers: {
'Content-Type':'application/x-www-form-urlencoded'
},
transformRequest: [function (data) {
let ret = '';
for (let it in data) {
ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
}
return ret;
}],
data:params
}).then(
function(response){
console.log('success:', response.data);
successCallback(response.data);
}
).catch(
function(error){
console.log("error:",error.response.data);
errorCallback(error.response.data);
}
);
}
function postJson(that,url,params,successCallback,errorCallback){
axios({
url:url,
method:"post",
headers: {
'Content-Type':'application/json'
},
transformRequest(data) {
return JSON.stringify(data);
},
data:params
}).then(
function(response){
console.log('success:', response.data);
successCallback(response.data);
}
).catch(
function(error){
console.log("error:",error.response.data);
errorCallback(error.response.data);
}
);
}
本站内容来源于作者发布和网络转载,如有版权相关问题请及时与我们取得联系,我们将立即删除。