: 네트워크에서 JSON 파일을 가져와서 콘솔에 출력함
https://developer.mozilla.org/ko/docs/Web/API/Fetch_API/Using_Fet
fetch('http://example.com/movies.json')
.then((response) => response.json())
.then((data) => console.log(data));
🔸 html
<div id="weather">
<span></span> <!-- 도시자리 -->
<span></span> <!-- 닐씨 자리 -->
</div>
🔸 js
const city = document.querySelector("#weather span:first-child");
const weather = document.querySelector("#weather span:last-child");
const API_KEY = "638932b0d124e5b26e3abba29abac448";
function onGeoOk(info) {
const lat = info.coords.latitude;
const lon = info.coords.longitude;
const part = info.coords.part;
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_KEY}&units=metric`;
//const url_2 = `https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&exclude=${part}&appid=${API_KEY}&units=metric`;
console.log(url);
//console.log(url_2);
fetch(url)
.then((response) => response.json())
.then((data) => {
city.innerText = `${data.name}\n`;
weather.innerText = `${data.weather[0].main} / ${data.main.temp}'`;
});
}
function onGeoErr() {
alert("위치를 찾을 수 없습니다. ");
}
navigator.geolocation.getCurrentPosition(onGeoOk, onGeoErr);