首页 > 解决方案 > 使用 Openweathermap API 未找到 http 错误

问题描述

我正在尝试编写一个根据您当前位置显示温度的天气应用程序,使用 OpenWeatherApi,我收到错误 404 我不知道为什么

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(position => {
      long = position.coords.longitude;
      lat = position.coords.latitude;
      console.log(position);
      const api = `api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=74913cd6ec927a299e7fd99534e17b5f`;
      fetch(api)
        .then(response => {
          return response.json();
        })
        .then(data => {
          console.log(data);
        });
    });

我在控制台中遇到的错误:

我在控制台中遇到的错误

标签: javascriptopenweathermap

解决方案


你没有达到他们的终点。由于您没有提供 URL 方案,因此它被识别为相对路径,并且您正在访问 localhost。


推荐阅读