我收到這些錯誤:
- 獲取http://127.0.0.1:5500/src/api.openweathermap.org/data/2.5/weather?lat=29.0188653&lon=77.7680952&appid=9268356eb17fbbe77a86201da74c9c46 404(未找到)
- Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
JavaScript
window.addEventListener("load", () => {
let lon;
let lat;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((postion) => {
lat = postion.coords.latitude;
lon = postion.coords.longitude;
const api = `api.openweathermap.org/data/2.5/weather?
lat=${lat}&lon=${lon}&appid=9268356eb17fbbe77a86201da74c9c46`;
fetch(api)
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);a
})
.catch((err) => alert("Enable your location"));
});
// else{
// alert("Geolocation is not supported by this browser.";);
// }
}
});
uj5u.com熱心網友回復:
嘗試像這樣將 https:// 添加到 URL
const api = `https://api.openweathermap.org/data/2.5/weather?
lat=${lat}&lon=${lon}&appid=9268356eb17fbbe77a86201da74c9c46`;
uj5u.com熱心網友回復:
您的本地開發環境應該是安全的,即您需要使用https://localhost或https://127.0.0.1檢索用戶的 lat 和 lnt 資料,在這種情況下,是您自己的。
使用它來設定在您的本地機器上創建一個 https 證書, https://github.com/FiloSottile/mkcert
此外,您需要此鏈接作為指南,https://www.section.io/engineering-education/how-to-get-ssl-https-for-localhost/
參考: https ://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition
navigator.geolocation.getCurrentPosition應該在一個安全的環境中,即使它是本地主機。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/426791.html
標籤:javascript json 获取 API
