프로그래밍 공부 메모/html
html with script
jjs815
2022. 5. 9. 23:05
- ajax (서버와 클라이언트 간의 데이터를 편한 from으로 주고(post) 받기(get)위한 라이브러리 )
기억해야 할것
1. 받아온 데이터에 접근시 배열 사용[ ] 필요에 따라 하위 데이터가 필요시 2차배열도 사용 [ ] [ ]
2. html파일에서 script로 테그를 끼워 넣고싶을 때 ` `(벡틱) 사용, 변수를 사용시 ${ }사용
사용예)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>jQuery 연습하고 가기!</title>
<!-- jQuery를 import 합니다 -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<style type="text/css">
div.question-box {
margin: 10px 0 20px 0;
}
.bad{
color: red;
}
</style>
<script>
function q1() {
$("#names-q1").empty()
$.ajax({
type: "GET",
url: "http://spartacodingclub.shop/sparta_api/seoulair",
data: {},
success: function(response){
let rows = response['RealtimeCityAir']['row']
for(let i = 0; i<rows.length; i++){
let gu_name = rows[i]['MSRSTE_NM']
let gu_mise = rows[i]['IDEX_MVL']
let temp_html = ``
if(gu_mise > 120){
temp_html = `<li class="bad">${gu_name} : ${gu_mise}</li>`
}else{
temp_html = `<li>${gu_name} : ${gu_mise}</li>`
}
$("#names-q1").append(temp_html)
//console.log(gu_name,gu_mise)
}
console.log(response['RealtimeCityAir']['row'])
}
})
}
</script>
</head>
<body>
<h1>jQuery+Ajax의 조합을 연습하자!</h1>
<hr />
<div class="question-box">
<h2>1. 서울시 OpenAPI(실시간 미세먼지 상태)를 이용하기</h2>
<p>모든 구의 미세먼지를 표기해주세요</p>
<p>업데이트 버튼을 누를 때마다 지웠다 새로 씌여져야 합니다.</p>
<button onclick="q1()">업데이트</button>
<ul id="names-q1">
</ul>
</div>
</body>
</html></title>
</head>
<body>
</body>
</html>
[서울 미세먼지 실시간 데이터] 활용
// http://spartacodingclub.shop/sparta_api/seoulair
{
"RealtimeCityAir": {
"RESULT": {
"CODE": "INFO-000",
"MESSAGE": "정상 처리되었습니다"
},
"list_total_count": 25,
"row": [
{
"ARPLT_MAIN": "O3",
"CO": 0.4,
"IDEX_MVL": 57.0,
"IDEX_NM": "보통",
"MSRDT": "202205092100",
"MSRRGN_NM": "도심권",
"MSRSTE_NM": "중구",
"NO2": 0.034,
"O3": 0.038,
"PM10": 32.0,
"PM25": 17.0,
"SO2": 0.003
}, ...
]
}
}
반응형