封装axios
程序员文章站
2022-03-11 21:55:53
import axios from 'axios'let baseURL = 'http://localhost:8090'const instance = axios.create({ baseURL: baseURL, timeout:7000, headers:{}})//请求拦截器instance.interceptors.request.use(function(config){ config.headers.Authorization = local...
import axios from 'axios'
let baseURL = 'http://localhost:8090'
const instance = axios.create({
baseURL: baseURL,
timeout:7000,
headers:{}
})
//请求拦截器
instance.interceptors.request.use(function(config){
config.headers.Authorization = localStorage.getItem('token') || ''
return config;
},function(error){
return Promise.reject(error);
})
//响应拦截器
instance.interceptors.response.use(function(response){
if(response.status === 200){
if(response.data && response.data.msg){
return response.data.data
}else{
alert('网络异常')
}
}
},function(error){
return Promise.reject(error)
})
export default instance
//封装api****************************
import axios from './fetch'
//首页商品列表(page,size)
export function fetchGoodList(params) {
return axios({
url:'/jd/getHotGoodList',
method:'GET',
params
})
}
//获取商品详情(good_id)
export function fetchGoodDetail(params){
return axios({
url: '/jd/getGoodDetail',
method:'GET',
params
})
}
//添加购物车
export function fetchAddToCart(data){
return axios({
url: '/jd/addToCart',
method: 'POST',
data
})
}
本文地址:https://blog.csdn.net/clear_fix/article/details/108818638