【发布时间】: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