[英]axios duplicates the url in the interceptor
I am using Axios for my HTTP request and this is my first time I got this problem.我在 HTTP 请求中使用 Axios,这是我第一次遇到这个问题。 the first request is passed and everything is good.
第一个请求通过,一切都很好。 but in my second request, I have a problem that Axios is duplicate my URL without reason.
但是在我的第二个请求中,我遇到了一个问题,即 Axios 无故重复了我的 URL。
the url that axios is trying to fetch at the end looks like this:最后 axios 尝试获取的 url 如下所示:
http://localhost:3001/http://localhost:3001/http://localhost:3001/user/5e4844f6451e0078e7dd398e
this is the fetch action:这是获取操作:
export const getUserDetailsById = async ({id}) => {
return await axios.get(`user/${id}`).then(res => res.data);
};
and the interceptor:和拦截器:
import axios from "axios";
export default () => {
axios.interceptors.request.use(function (config) {
const token = localStorage.getItem(process.env.REACT_APP_TOKEN_NAME);
config.headers.common['authorization'] = `Bearer ${token}`;
config.headers.common['Content-Type'] = 'application/json';
config.url = `${process.env.REACT_APP_API_URL}/${config.url}`;
const url = config.url;
console.log(url);
return config;
}, function (error) {
return Promise.reject(error);
});
axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
return Promise.reject(error);
});
};
You can set baseURL
when calling axios.create
您可以设置
baseURL
调用时axios.create
import axios from 'axios';
var api = axios.create({
baseURL: `${AppConfig.apiUrl}/services`,
});
export default api;
then import api
when ever you want to use axios.然后在您想使用 axios 时导入
api
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.