import { baseUrl } from './env' //**引入全局配置请求后台接口的基础URL配置**
import axios from 'axios' // **引入axios**
import { Notification } from 'element-ui' // **引入element的通知组件**
axios.defaults.baseURL = baseUrl //**配置axios 的全局默认基础URL,注意区分axios.defaults.baseURL 的大小写**
axios.defaults.headers.post['X-Request-with'] = 'XMLHttpRequest' //****配置axios 的全局默认请求头Ajax 异步请求****
export default async(url, method, data = {}, forbid) => {
let response
if (sessionStorage.getItem('token')) {
axios.defaults.headers.common.Authorization = sessionStorage.getItem('token') // 配置axios携带token信息返回给后端
}
if (method == 'get') {
const dat = {
params: data
}
response = await axios.get(url, dat)
} else if (method == 'post') {
response = await axios.post(url, data)
}O
if (response.data.res === 1) {
if (!forbid) {
Notification.success({
message: response.data.msg,
duration: 1500
})
}
return Promise.resolve(response.data.dat)
} else if (response.data.res === 0) {
Notification.error({
message: response.data.msg,
duration: 5000
})
return Promise.reject(response.data.msg)
} else {
Notification.error({
message: response.data.msg,
duration: 1500
})
return Promise.reject(response.data.msg)
}
}
axios和element 组件配合请求后端接口的框架代码
最新推荐文章于 2024-08-09 21:02:54 发布