๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐ŸŒ  ์ž๋ฐ” ์„œ๋ฒ„ ํŽ˜์ด์ง€ JSP/๐Ÿ›ธ ์ œ์ด์Šจ XML\JSON

2๏ธโƒฃ XML - readyState ์ƒํƒœ, status/stateText ์„œ๋ฒ„ ์‘๋‹ต์ƒํƒœ

by Meteora_ 2021. 3. 9.
728x90

 

onreadyStateChange์— ๋“ฑ๋ก๋œ callback ํ•จ์ˆ˜๋Š” readyState๋ผ๋Š” ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋œ๋‹ค.

 

์„œ๋ฒ„์—์„œ ์‘๋‹ต์ด ์˜ฌ๋•Œ function์„ ์ˆ˜ํ–‰ํ•˜๊ณ ์ž ํ•  ๊ฒฝ์šฐ

-์„œ๋ฒ„์—์„œ ์‘๋‹ต์ด ์˜ฌ๋•Œ ์ˆ˜ํ–‰ํ•  function์„ onreadyState์— ๋“ฑ๋ก ํ›„ ์‹ค์ œ ์‘๋‹ต์ด ์˜ค๋ฉด(readyState๊ฐ’์œผ๋กœ ํŒ๋‹จ)

๋“ฑ๋กํ•œ function์„ ์‹คํ–‰ํ•œ๋‹ค.

-httpRequest.onreadystatechange=callbackFunction;

 

-์‹ค์ œ ์‹คํ–‰ํ•˜๋Š” function์—์„œ๋Š” ์ƒํƒœ์˜ ๋ณ€ํ™”๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” readyState์ƒํƒœ์— ๋”ฐ๋ผ ์‘๋‹ต ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค

 

httpRequest.readyState == 4 ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋ถ€ ๋ฐ›์€ ์ƒํƒœ์ด๊ณ  ์™„๋ฃŒ๋˜์—ˆ๋‹ค๋Š” ๋œป์ด๊ณ 

httpRequest.status == 200๋Š” ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์‘๋‹ต์š”์ฒญ์ด ์„ฑ๊ณตํ–ˆ๋‹ค๋Š” ๋œป์ด๋‹ค.

 

 

 

 

์›น์ฝ˜ํ…์ธ ์— text.txt ํŒŒ์ผ ์ถ”๊ฐ€๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<p id="demo">p tag</p>

<button type="button" onclick="loadXMLDoc()">๋‚ด์šฉ๋ณ€๊ฒฝ</button>

<script type="text/javascript">

let xhttp = new XMLHttpRequest();		//XML ํŒŒ์ผ ์ฝ๊ธฐ 	Request : ์š”์ฒญ

function loadXMLDoc(){
	
	xhttp.onreadystatechange = function () {
		
		//console.log( this.responseText );		//response : ์‘๋‹ต
		//console.log(this.status);
		//console.log(this.readyState);
		
		if(this.readyState == 4 && this.status == 200){
			document.getElementById("demo").innerHTML = this.responseText;
		}
		
		
	}
	xhttp.open("GET", "test.txt", true);
	console.log("xhttp.open");
	
	xhttp.send();
	console.log("xhttp.send");
	
}


/* readyState : ์ง„ํ–‰์ƒํƒœ
	0 -> open ๋ฉ”์†Œ๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ „ ์ƒํƒœ
	1 -> loading ์ค‘..
	2 -> loading ์™„๋ฃŒ
	3 -> Server ์ฒ˜๋ฆฌ์ค‘
	4 -> Server ์ฒ˜๋ฆฌ ์™„๋ฃŒ
	
	status
	200 -> success
	403 -> ์ ‘๊ทผ ๊ธˆ์ง€
	404 -> ์—†์Œ, ๋ชป์ฐพ๊ฒ ์Œ
	500 -> ๊ตฌ๋ฌธ์—๋Ÿฌ
	
*/



</script>

</body>
</html>

 

๋Œ“๊ธ€