axios删除/在获取请求之前

问题描述 投票:0回答:2

我正在使用

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插入/之后,有没有办法禁用它?它违反了我的要求。

reactjs redux get axios
2个回答
0
投票

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;
});

0
投票

您可以使用get中的params选项传递params。

const res = await weatherApi.get({
  params: {
     lat: lat,
     lon: lon,
     appid: KEY,
     units: unit
  }
);

axios get请求可以轻松处理参数。

并且还要从您的基本URL中删除?

© www.soinside.com 2019 - 2024. All rights reserved.