简体   繁体   English

未捕获的类型错误:无法获取

[英]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.

相关问题 获取 API -> Uncaught (in promise) TypeError: Failed to fetch - Fetch an API -> Uncaught (in promise) TypeError: Failed to fetch Service Worker - 未捕获(承诺)TypeError:无法获取 - Service Worker - Uncaught (in promise) TypeError: Failed to fetch Uncaught (in promise) TypeError: Failed to fetch and Cors error - Uncaught (in promise) TypeError: Failed to fetch and Cors error Progressive Web App Uncaught (in promise) TypeError: Failed to fetch - Progressive web app Uncaught (in promise) TypeError: Failed to fetch Uncaught (in promise) TypeError: Failed to fetch - Service Worker with workbox - Uncaught (in promise) TypeError: Failed to fetch - Service worker with workbox net::ERR_CONNECTION_RESET, Uncaught (in promise) TypeError: Failed to fetch - net::ERR_CONNECTION_RESET, Uncaught (in promise) TypeError: Failed to fetch net :: ERR_EMPTY_RESPONSE &amp;&amp;未捕获(承诺中)TypeError:无法获取 - net::ERR_EMPTY_RESPONSE && Uncaught (in promise) TypeError: Failed to fetch dataform.js:49 Uncaught (in promise) TypeError: Failed to fetch - dataform.js:49 Uncaught (in promise) TypeError: Failed to fetch Uncaught (In Promise) TypeError: Failed to Fetch - In Service worker when offline - Uncaught (In Promise) TypeError: Failed to Fetch - In Service worker when offline 我有这个错误 Uncaught (in promise) TypeError: Failed to fetch - i have this error Uncaught (in promise) TypeError: Failed to fetch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM