我正在使用
export default axios.create({
baseURL: `https://api.openweathermap.org/data/2.5/onecall?`,
responseType: "json",
});
然后响应将其附加到请求中
const res = await weatherApi.get(
`lat=${lat}&lon=${lon}&appid=${KEY}&units=${unit}`
);
但最终结果是https://api.openweathermap.org/data/2.5/onecall?/lat=53&lon=1.15&appid=keyremoved&units=metric
在onecall?
axios插入/
之后,有没有办法禁用它?它违反了我的要求。
https://github.com/axios/axios/issues/757#issuecomment-291016487
您可以为此使用axios拦截器,但是如果您厌倦了此操作会修改您的所有请求,则可能需要对何时执行此操作进行更仔细的检查!
axios.interceptors.request.use((config) => {
if (config.url[config.url.length - 1] === '/') {
config.url = config.url.substr(0, str.length - 1);
}
return config;
});
您可以使用get中的params选项传递params。
const res = await weatherApi.get({
params: {
lat: lat,
lon: lon,
appid: KEY,
units: unit
}
);
axios get请求可以轻松处理参数。
并且还要从您的基本URL中删除?
。