在项目中载入axios
项目根目录下 :
npm install axios
1、创建api文件夹
2、http.js中写入
import axios from 'axios'
import router from '../router'
axios.defaults.timeout = 5000 // 超时时间5秒
axios.defaults.withCredentials = true // 允许跨域
// 访问方式
// Content-type响应头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
// 基础url
axios.defaults.baseURL = 'http://localhost:8099'
// 响应拦截器
axios.interceptors.response.use(
response => {
// 如果response的status是200 说明访问到借口了,否则错误
if (response.status === 200) {
return Promise.resolve(response)
} else {
return Promise.reject(response)
}
},
error => {
if (error.response.status) {
switch (error.respon