使用 Axios 的 HTTP get 请求以 URL 开头的本地主机 IP 发送 [英] HTTP get request with Axios gets sent with the local host IP at the beginning of the URL

查看:43
本文介绍了使用 Axios 的 HTTP get 请求以 URL 开头的本地主机 IP 发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Axios 发送 HTTP 请求,但收到 404 错误.原因是请求以 URL 开头的本地主机 IP 发送,为什么会发生这种情况?

I am trying to sent an HTTP request with Axios, but I get a 404 error. The reason is that the request gets sent with the local host IP at the beginning of the URL, why is this happening?

JS:

function getWeather() {

  axios.get('api.openweathermap.org/data/2.5/weather', {
    params: {
      lat: 30.18,
      lon: 30.87,
      appid: '57d9478bc08bc211f405f45b93b79272'
    }
  })
  .then(function(response) {
    console.log(response);
  })

  .catch(function(error) {
    console.log(error);
  })
};
getWeather();  

错误:

http://127.0.0.1:5500/api.openweathermap.org/data/2.5/weather?lat=30.18&lon=30.87&appid=57d9478b#####################3b79272 404 (Not Found)

推荐答案

在 Axios 的 URL 参数中,您没有指定用于 API 请求(可能是 HTTP)的协议.因此,Axios 将其解释为相对 URL 路径并添加本地服务器的路径,因为它需要一个完整的 URL 来发出请求.

In the URL argument for Axios, you are not specifying the protocol to use for your API request (probably HTTP). Because of that, Axios interprets it as a relative URL path and adds the path of your local server, because it needs a full URL to make the request.

您可以通过添加 http://前缀轻松修复它:

You can easily fix it by adding the http:// prefix:

axios.get('http://api.openweathermap.org/data/2.5/weather', {

这篇关于使用 Axios 的 HTTP get 请求以 URL 开头的本地主机 IP 发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆