【问题标题】:Uncaught TypeError: Failed to fetch未捕获的类型错误:无法获取
【发布时间】:2026-01-26 14:30:01
【问题描述】:

编辑: 我修好了答案在答案部分。

我正在尝试创建一个天气应用程序,但我偶然发现了一个错误。我尝试从我正在使用的 API(openweathermap.org)中获取,我知道我正在获取的地址是正确的,因为当我在浏览器中打开它时会加载 .json。我很困惑为什么它无法获取。

这是我的代码

<html>
<body>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else { 
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {
    const lat = position.coords.latitude
    const lon = position.coords.longitude
    console.log(lat,lon)
    const API = "Im using open"
    fetch("api.openweathermap.org/data/2.5/weather?lat="+ lat +"&lon="+ lon +"&appid="+ API)
}
</script>

</body>
</html>

【问题讨论】:

    标签: javascript fetch openweathermap


    【解决方案1】:

    我刚刚意识到我在获取的开头没有“https://”。

    【讨论】:

      最近更新 更多