๐Ÿ’ป

JavaScript ์˜ ์ดํ•ด - [AJAX] JSON ๊ฐ์ฒด ๋‹ค๋ฃจ๊ธฐ ๋ณธ๋ฌธ

KITRI/JAVASCRIPT

JavaScript ์˜ ์ดํ•ด - [AJAX] JSON ๊ฐ์ฒด ๋‹ค๋ฃจ๊ธฐ

๋˜ํšจ๋‹ˆ 2020. 7. 15. 16:50

Ajax๋ž€?

AJAX๋ž€ ๋น„๋™๊ธฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€ XML (Asynchronous JavaScript And XML)์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๊ฐ„๋‹จํžˆ ๋งํ•˜๋ฉด, ์„œ๋ฒ„์™€ ํ†ต์‹ ํ•˜๊ธฐ ์œ„ํ•ด XMLHttpRequest ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. JSON, XML, HTML ๊ทธ๋ฆฌ๊ณ  ์ผ๋ฐ˜ ํ…์ŠคํŠธ ํ˜•์‹ ๋“ฑ์„ ํฌํ•จํ•œ ๋‹ค์–‘ํ•œ ํฌ๋งท์„ ์ฃผ๊ณ  ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. AJAX์˜ ๊ฐ•๋ ฅํ•œ ํŠน์ง•์€ ํŽ˜์ด์ง€ ์ „์ฒด๋ฅผ ๋ฆฌํ”„๋ ˆ์‰ฌ ํ•˜์ง€ ์•Š๊ณ ์„œ๋„ ์ˆ˜ํ–‰ ๋˜๋Š” "๋น„๋™๊ธฐ์„ฑ"์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋น„๋™๊ธฐ์„ฑ์„ ํ†ตํ•ด ์‚ฌ์šฉ์ž์˜ Event๊ฐ€ ์žˆ์œผ๋ฉด ์ „์ฒด ํŽ˜์ด์ง€๊ฐ€ ์•„๋‹Œ ์ผ๋ถ€๋ถ„๋งŒ์„ ์—…๋ฐ์ดํŠธ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.

 

AJAX์˜ ์ฃผ์š” ๋‘๊ฐ€์ง€ ํŠน์ง•

  1. ํŽ˜์ด์ง€ ์ƒˆ๋กœ๊ณ ์นจ ์—†์ด ์„œ๋ฒ„์— ์š”์ฒญ
  2. ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๊ณ  ์ž‘์—…์„ ์ˆ˜ํ–‰

 

Ajax๋Š” ์–ธ์–ด๋‚˜ ํ”„๋ ˆ์ž„ ์›Œํฌ๊ฐ€ ์•„๋‹Œ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ์‹์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์ด๋™ํ•˜๊ณ  ํ™”๋ฉด์„ ๊ตฌ์„ฑํ•˜๋Š”๋ฐ ์žˆ์–ด์„œ ์›น ํ™”๋ฉด์„ ๊ฐฑ์‹ ํ•˜์ง€ ์•Š๊ณ  ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„๋กœ ๋ณด๋‚ด๊ณ  ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ํ™”๋ฉด ๊ฐฑ์‹ ์ด ์—†์–ด์„œ ์‚ฌ์šฉ์ž ์ž…์žฅ์—์„œ๋Š” ๋งค์šฐ ํŽธ๋ฆฌํ•˜๊ณ  ๋น ๋ฅด๊ฒŒ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋Š๋ผ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๋™์ ์œผ๋กœ ํ™”๋ฉด์„ ๊ตฌ์„ฑํ•˜๋Š” ๋งŒํผ ๊ฐœ๋ฐœ์ž์˜ ๊ตฌํ˜„์€ ๋ณต์žกํ•ด ์ง‘๋‹ˆ๋‹ค.

 

 

JavaScript ์—์„œ์˜ Ajax ๊ตฌํ˜„ ๋ฐฉ๋ฒ•

 

https://developer.mozilla.org/ko/docs/Web/Guide/AJAX/Getting_Started

 

Ajax ์‹œ์ž‘ํ•˜๊ธฐ

๋ณธ ๋ฌธ์„œ๋Š” AJAX์˜ ๊ธฐ๋ณธ์„ ์ตํž์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋ฉฐ, ๋‘ ๊ฐ€์ง€ ๊ฐ„๋‹จํ•œ ํ›ˆ๋ จ์šฉ ์˜ˆ์ œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

developer.mozilla.org

 


json01.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="json01.js"></script>
</head>
<body onload="toServer()">
	<div id="disp"></div>
</body>
</html>

json01.js

/**
 * 
 */
var xhr = null;
var arr = new Array();

function toServer(){
	if(window.XMLHttpRequest){
		xhr = new XMLHttpRequest;
	}else{
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xhr.open("GET", "json01.txt", true);
	xhr.send();
	xhr.onreadystatechange = resultProcess;
}
function resultProcess(){
	if(xhr.readyState==4 && xhr.status==200){
		arr.push(xhr.responseText);
		//alert(arr);
		
		var obj = JSON.parse(xhr.responseText);
		
		var disp = document.getElementById("disp");
		var ul = document.createElement("ul");
		
		var liNode1 = document.createElement("li");
		liNode1.innerHTML = obj.name;
		
		var liNode2 = document.createElement("li");
		liNode2.innerHTML = obj.age;
		
		var liNode3 = document.createElement("li");
		liNode3.innerHTML = obj.ki;
		
		ul.appendChild(liNode1);
		ul.appendChild(liNode2);
		ul.appendChild(liNode3);
		disp.appendChild(ul);
	}
}

 

json01.txt

์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์„ ๋•Œ jsonํŒŒ์ผ๋กœ ๋ฐ›์ง€๋งŒ ์—ฌ๊ธฐ์„œ ์—ฐ์Šต์„ ์œ„ํ•ด jsonํ˜•์‹์œผ๋กœ .txt ํŒŒ์ผ์— ์ž‘์„ฑํ•˜์˜€๋‹ค.

{
  "name": "ํ™๊ธธ๋™",
  "age": "10",
  "ki": "170.5"
}

 

 

 

 

 

 

json02.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="json02.js"></script>
</head>
<body onload="toServer()">
	<div id="disp"></div>
</body>
</html>

 

json02.js

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="json02.js"></script>
</head>
<body onload="toServer()">
	<div id="disp"></div>
</body>
</html>

 

json02.txt

๊ฐ์ฒด๋Š” {}์— ๋„ฃ์–ด์ฃผ๊ณ , ๋ฐฐ์—ด์€ []์— ๋‹ด์•„์ค€๋‹ค.

{
	"name": "ํ™๊ธธ๋™",
	"age" : "30",
	"cars":[
		{	"name": "Ford",
		 	"model": ["Fiesta", "Focus", "Mustang"]
		},
		
		{	"name": "BMW",
		 	"model": ["320", "X3", "X5"]
		},
		
		{	"name": "hyundai",
		 	"model": ["sonata", "santafe", "avante"]
		}
	]
}

 

๋ฐ˜์‘ํ˜•
Comments