当前位置:首页 > 操作系统 > Ios

javascript – 使用axios的http get请求在url上使用本地主机ip发送它

我试图用axios发送http请求,但我得到404错误,原因是请求在url的开头用本地主机ip发送,为什么会发生这种情况?

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来发出请求.

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

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

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!