[英]Why is axios Library failing to make a request when an API call doesn't start with "https://"?
This is not an issue to me anymore, as I solved this problem, but I still want to ask it to better understand what is happening under the hood.这对我来说不再是问题,因为我解决了这个问题,但我仍然想问它以更好地了解引擎盖下发生的事情。 So, I use an API to fetch data about current weather in a specific city.
因此,我使用 API 来获取有关特定城市当前天气的数据。 The call ( according to the API provider's documentation ) is as follows:
调用(根据 API 提供商的文档)如下:
axios.get(`api.openweathermap.org/data/2.5/weather?q=${countryCapital}&appid=%{API_KEY}`)
Such request fails.这样的请求失败了。 The console shows that the request was made to an address that has
http://localhost:3000/
appended in front of it, which is why it fails.控制台显示请求是对一个地址发出的,该地址前面附加了
http://localhost:3000/
,这就是它失败的原因。 When I modify the API call to:当我将 API 调用修改为:
axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${countryCapital}&appid=%{API_KEY}`)
then everything works as intended.然后一切都按预期工作。 Why is that so?
为什么会这样?
When you don't specify the scheme, the URL would be a relative URL!当您不指定方案时,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.