get
参数直接拼接url
通过params对象传参 (后端使用req.query接收)
另外:restful形式的url api/:id (后端使用req.params接收)
axios.get(url,{
params:{
id:1
}
})
delete
和get请求类似
post
参数传递(后端使用req.body接收)
默认是json格式
请求头信息:Content-Type: application/json;charset=UTF-8
axios.post(url,{
name:zs,
password:zs
})
表单格式
请求头信息:Content-Type: application/x-www-form-urlencoded;charset=UTF-8
// 模拟表单传递 x/www-form-urlencoded
var params=new URLSearchParams()
params.append('phone',777)
params.append('pwd',777)
this.$axios.post("/api/login",params).then(res=>{
console.log(res);
})
put
和post类似,支持两种格式json和表单
axios响应信息
data数据
headers响应头
status状态码
statusText状态信息
axios全局配置
设置请求超时
axios.defaults.timeout=3000
设置默认地址
axios.defaults.baseURL=默认地址
设置请求头
axios.defaults.headers=请求头对象
axios拦截器
必须在拦截器中return config 或者 res
// 请求拦截器
axios.interceptors.request.use(config=>{
config.headers.token='Bearer asd'
return config
},err=>{
console.log(err);
})
// 响应拦截器
axios.interceptors.response.use(res=>{
let data=res.data
return data
},err=>{
console.log(err);
})