axios核心
安装命令 cnpm i axios --save
axios.request()
//一个完整的get请求
axios.get("",{
//需要提交给后台的数据
params:{
userID:""
},
//请求头中携带token进行后台验证
headers:{
token:''
},
before:function(){
console.log("此方法会在请求之前调用")
}
}).then(res=>{ //成功回调函数
console.log(res)
}).catch(function (error){ //请求失败的回调函数
console.log(error)
})
//一个完整的post请求,与get不同的是不用params去提交参数,直接定义就好
axios.post("",{
userId:''
},{
headers:{
token:"",
}
}).then(res=>{
console.log(res)
}).catch(function (error){
console.log(error)
})
//前两种是缩写写法,另一种写法
axios({
url:"/api/login", //请求的地址
method:"get", //请求方法
data:{ //post方法直接在data里面定义就可以
userId:""
},
params:{ //使用get发送请求时一定得用params提交
userId:""
},
headers:{ //请求头
token:token
}
}).then(res=>{ //成功回调函数
console.log(res)
}).catch(function(error){
console.log(error)
})
axios全局拦截器用法
mounted:function(){
axios.interceptors.request.use(function(config){
console.log("所有发送request请求都要先执行此方法")
return config;
})
axios.interceptors.response.use(function(response){
console.log("所有发送response请求都要先执行此方法")
return response;
})
}
axios请求方式详解
最新推荐文章于 2024-08-12 17:01:01 发布