繁体   English   中英

当 API 调用不是以“https://”开头时,为什么 axios 库无法发出请求?

[英]Why is axios Library failing to make a request when an API call doesn't start with "https://"?

这对我来说不再是问题,因为我解决了这个问题,但我仍然想问它以更好地了解引擎盖下发生的事情。 因此,我使用 API 来获取有关特定城市当前天气的数据。 调用(根据 API 提供商的文档)如下:

axios.get(`api.openweathermap.org/data/2.5/weather?q=${countryCapital}&appid=%{API_KEY}`)

这样的请求失败了。 控制台显示请求是对一个地址发出的,该地址前面附加了http://localhost:3000/ ,这就是它失败的原因。 当我将 API 调用修改为:

axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${countryCapital}&appid=%{API_KEY}`)

然后一切都按预期工作。 为什么会这样?

当您不指定方案时,URL 将是一个相对 URL!

  // `url` is the server URL that will be used for the request
  url: '/user',

  // `method` is the request method to be used when making the request
  method: 'get', // default

  // `baseURL` will be prepended to `url` unless `url` is absolute.
  // It can be convenient to set `baseURL` for an instance of axios to pass relative URLs
  // to methods of that instance.
  baseURL: 'https://some-domain.com/api/',

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM