[英]Uncaught TypeError: Failed to fetch
Edit: I fixed it the answer is in the answer section.编辑:我修复了它,答案在答案部分。
I have trying to create a weather application and I have stumbled on a bug.我试图创建一个天气应用程序,但我偶然发现了一个错误。 I try to fetch from the API I'm using which is openweathermap.org, I know the address that I'm fetching is correct because when I open it in my browser the .json loads.我尝试从我正在使用的 API openweathermap.org 中获取,我知道我获取的地址是正确的,因为当我在浏览器中打开它时会加载 .json。 I'm confused about why it fails to fetch.我很困惑为什么它无法获取。
Here is my code这是我的代码
<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>
我刚刚意识到我在获取开始时没有“https://”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.