axios
基本概念APIaxios.request(config)axios.get(url[, config])axios.delete(url[, config])axios.head(url[, config])axios.post(url[, data[, config]])axios.put(url[, data[, config]])axios.p...
·
基本概念
API
axios.request(config)
axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])
特别补充
axios.all() 同时获得两个接口
实例
function getUserAccount(){
return axios.get('/user/12345');
}
function getUserPermissions(){
return axios.get('/user/12345/permissions');
}
axios.all([getUserAccount(),getUserPermissions()])
.then(axios.spread(function(acct,perms){
}));
Demo演示
GET
get(){
axios.get("package1.json",{
//传参
params: {
userId: "999"
},
headers: {
token: "tim"
},
})
.then(res=>{
this.msg = res.data;
})
//注意这里,失败是通过catch进行捕获的
.catch(function(error){
console.log("显示错误信息" + error);
});
}
POST
post(){
axios.post("package.json",{
//直接定义参数
userId: "888"
},
//这里定义选项配置
{
headers: {
token: "chen"
}
})
.then(res=>{
this.msg = res.data;
})
//一样是通过catch进行捕获
.catch(function(error){
console.log("显示错误信息" + error);
})
}
全局拦截
axios.interinterceptors.request.use(callback(config)) 发送请求前
axios.interceptors.response.use(callback(response)) 响应结束后
实例
axios.interceptors.request.use(function(config){
console.log("requset请求前");
return config;
})
axios.interceptors.response.use(function(response){
console.log("response响应后");
return response;
})
更多推荐
已为社区贡献1条内容
所有评论(0)