
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<title>AJAX로 현재 날씨정보를 얻어와요</title>
<script>
let c_name = $('#city');
let img = $('#img');
let r_temp = $('#r_temp');
$(document).ready(function(){
});
function p1() {
$.ajax({
type: "GET",
url: "http://spartacodingclub.shop/sparta_api/weather/seoul",
data: {},
success: function (response) {
console.log(response);
let city = response['city'];
let url = response['icon'];
let temp = response['temp'];
c_name.empty();
img.empty();
r_temp.empty();
$('#city').text(city);
img.attr("src", url);
r_temp.text(temp);
console.log(city);
}
})
}
</script>
</head>
<body>
<div>
-<span id="city">1</span>
<span id="r_temp">2</span>
<img id="img">
<button onclick="p1()">누르기</button>
</div>
</body>
</html>
$('#city').text(city);
img.attr("src", url);
r_temp.text(temp);
이 구분에서
p1 함수 전에
let c_name = $('#city');
let img = $('#img');
let r_temp = $('#r_temp');
전역변수로 3개를 할당해 놓은 것 같은데
결과창에서는 읽지 못해서요
혹시 전역변수를 이렇게 할당한게 맞나요.. ㅜㅜ?
