๐Ÿ‘€ [JavaScript] Ajax ์‚ฌ์šฉ๋ฒ• ์•Œ์•„๋ณด๊ธฐ

์ „์ฃผ์€ยท2022๋…„ 11์›” 23์ผ
0
post-thumbnail

๐Ÿ™„ Ajax ์‚ฌ์šฉ๋ฒ• ์•Œ์•„๋ณด๊ธฐ

โœ” HTTP request์„ ์–ด๋–ป๊ฒŒ ๋งŒ๋“œ๋Š”๊ฐ€

Ajax์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” XMLHttpRequest ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋ฒ„๋กœ ์š”์ฒญ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์š”์ฒญ์‚ฌํ•ญ(request)์„ XMLHttpRequest ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค.

const httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'http://www.example.org/some.file', true);
httpRequest.send();

์—ฌ๊ธฐ์„œ open()๋ฉ”์†Œ๋“œ๋Š” 3๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ, ์ฒซ๋ฒˆ์งธ๋Š” get,post,head,.. ๋“ฑ์˜ ์š”์ฒญ๋ฐฉ๋ฒ•์„ ๊ฒฐ์ •ํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ๋‘๋ฒˆ์งธ๋Š” ์š”์ฒญ์„ ๋ณด๋‚ผ URL์ž…๋‹ˆ๋‹ค. ์„ธ๋ฒˆ์งธ๋Š” optional parameter๋กœ, default๊ฐ’์€ true์ž…๋‹ˆ๋‹ค. ๋น„๋™๊ธฐ์ ์ธ์ฒ˜๋ฆฌ๋ฅผ ํ• ๊ฒƒ์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ๊ฒฐ์ •ํ•œ๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค.
send()๋ฉ”์†Œ๋“œ๋Š” ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ()์•ˆ์— ๋„ฃ์–ด ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ post๋ฐฉ์‹์œผ๋กœ ๋ณด๋‚ด๊ณ  ์‹ถ๋‹ค๋ฉด,์„œ๋ฒ„๊ฐ€ ์•Œ์•„๋“ค์„ ์ˆ˜ ์žˆ๋Š” query string์ด๋‚˜ XML,JSON๊ฐ™์€ ๊ฒƒ์„ ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ ๋‚˜์„œ, ์„œ๋ฒ„์— ์š”์ฒญํ•œ ๊ฒƒ์— ๋Œ€ํ•œ ์ƒํƒœ๋ณ€ํ™”๋ฅผ ์•Œ๋ ค์ฃผ๋Š” onreadystatechange property๋ฅผ ์ด์šฉํ•˜์—ฌ ๋Œ์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

function handler() {
  //๋Œ์•„์˜จ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณณ
}

httpRequest.onreadystatechange = handler;

์ด๋•Œ ํ•จ์ˆ˜ ์ด๋ฆ„์ด ํ•„์š”์—†๊ธฐ ๋•Œ๋ฌธ์— ์ต๋ช…ํ•จ์ˆ˜๋กœ ์จ๋„ ์ƒ๊ด€์—†์Šต๋‹ˆ๋‹ค.

httpRequest.onreadystatechange = () => {
    //๋Œ์•„์˜จ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณณ
};

์ž ์ด์ œ ์ด๊ฒƒ๋“ค์„ ํ•ฉ์ณ์„œ ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค!

const httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'http://www.example.org/some.file', true);
httpRequest.send();
httpRequest.onreadystatechange = () => {
    //๋Œ์•„์˜จ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณณ
};

โœ” ์„œ๋ฒ„์—๊ฒŒ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š”๊ฐ€?

์ด์ œ ์ด ์•ˆ์—๋“ค์–ด๊ฐˆ ๋‚ด์šฉ์„ ํ•œ๋ฒˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

httpRequest.onreadystatechange = () => {
    //๋Œ์•„์˜จ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณณ!! ๋ฐ”๋กœ ์ด๊ณณ!!
};

๋‹น์—ฐํžˆ ์„œ๋ฒ„์—์„œ ์š”์ฒญ์ด ์™„๋ฃŒ๋˜๊ณ  response๊ฐ€ ์ค€๋น„ ๋˜์—ˆ์„๋•Œ(httpRequest.readyState === XMLHttpRequest.DONE) ๊ทธ๋ฆฌ๊ณ  HTTP ์‘๋‹ต์ƒํƒœ ์ฝ”๋“œ๋„ ์ •์ƒ์ ์ผ๋•Œ(httpRequest.status === 200), ์šฐ๋ฆฌ๋Š” ์›นํŽ˜์ด์ง€ ์ผ๋ถ€ ๋‚ด์šฉ์„ ํ‹ฐ์•ˆ๋‚˜๊ฒŒ ๋ฐ”๊พธ๊ณ  ์‹ถ๊ธฐ๋•Œ๋ฌธ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์กฐ๊ฑด๋ฌธ์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

httpRequest.onreadystatechange = () => {
    if (httpRequest.readyState === XMLHttpRequest.DONE && httpRequest.status === 200) {
    //์„ฑ๊ณต ํ–ˆ์„๋•Œ
	} else {
   	//์‹คํŒจ ํ–ˆ์„๋•Œ
	}
};

XMLHTTPRequest.readyState

0 (uninitialized) or (request not initialized)
1 (loading) or (server connection established)
2 (loaded) or (request received)
3 (interactive) or (processing request)
4 (complete) or (request finished and response is ready) = XMLHttpRequest.DONE

HTTP response status codes

Informational responses (100 โ€“ 199)
Successful responses (200 โ€“ 299)
Redirection messages (300 โ€“ 399)
Client error responses (400 โ€“ 499)
Server error responses (500 โ€“ 599)

์ฐธ๊ณ ๋กœ ์กฐ๊ฑด๋ฌธ์—์„œ &&์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด httpRequest.readyState 0,1,2,3๋‹จ๊ณ„๋ฅผ ์ƒ๋žตํ•  ์ˆ˜์žˆ์–ด์„œ ์•ž์— ๋‘๋Š” ๊ฒƒ์ด ๋” ํšจ์œจ์  ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

โœ” ์˜ˆ์ œ๋กœ ์ดํ•ดํ•ด๋ณด๊ธฐ - get

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Ajax</title>
<script>
	function sendRequest(){
		const xhr = new XMLHttpRequest();
		xhr.open('get','4_ajax_ok.jsp?userid=apple&userpw=1234',true);
		xhr.send();
		
		xhr.onreadystatechange = function(){
			//๋Œ์•„์˜จ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณณ
			//XMLHttpRequest.DONE -> readyState:4
			
			if(xhr.readyState == XMLHttpRequest.DONE && xhr.status ==200){
				document.getElementById('text').innerHTML = xhr.responseText;
				
			}	
		}
	}
</script>
</head>
<body>
	<h2>Ajax</h2>
	<p><button onclick="sendRequest()">์š”์ฒญ ๋ณด๋‚ด๊ธฐ</button></p>
	<hr>
	<p id="text"></p>
</body>
</html>
//ajax_ok์ฝ”๋“œ
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String userid = request.getParameter("userid");
	String userpw = request.getParameter("userpw");
	
	out.println("์•„์ด๋””๋Š”"+userid+"์ด๋ฉฐ, ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š”"+ userpw + "์ž…๋‹ˆ๋‹ค.");
%>
  1. ์‚ฌ์šฉ์ž๊ฐ€ "์š”์ฒญ ๋ณด๋‚ด๊ธฐ"๋ฒ„ํŠผ์„ ๋ˆ„๋ฆ…๋‹ˆ๋‹ค.
  2. ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ sendRequest()ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
  3. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” XMLHttpRequest ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋ฒ„๋กœ ์š”์ฒญ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค.
    • get๋ฐฉ์‹์„ ํ†ตํ•ด url์— userid์™€ userpw๋ฅผ ๋ณด๋ƒˆ์Šต๋‹ˆ๋‹ค.
  4. ์„œ๋ฒ„๋Š” ์ฒ˜๋ฆฌํ•œ ๊ฒฐ๊ณผ๋ฅผ ํ…์ŠคํŠธ๋กœ ์›น ๋ธŒ๋ผ์šฐ์ €์— ์ „๋‹ฌ ํ•ฉ๋‹ˆ๋‹ค.
  5. document.getElementById('text').innerHTML = xhr.responseText;
    ํ•ด๋‹น ํ…์ŠคํŠธ๋Š” innerHTML์„ ํ†ตํ•ด text id๋ฅผ ๊ฐ€์ง„ ์š”์†Œ์— ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค

โœ” ์˜ˆ์ œ๋กœ ์ดํ•ดํ•ด๋ณด๊ธฐ - post

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Ajax</title>
<script>
  
  	document.getElementById("ajaxButton").onclick = () => {
  	const userName = document.getElementById("ajaxTextbox").value;
 	makeRequest('test.php', userName);
	};

	function makeRequest(url, userName) {
        httpRequest = new XMLHttpRequest();

      	if (!httpRequest) {
        	alert("Giving up :( Cannot create an XMLHTTP instance");
        	return false;
      	}
      
 		httpRequest.onreadystatechange = alertContents;
 		httpRequest.open('POST', url);
    	httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
 		httpRequest.send(`userName=${encodeURIComponent(userName)}`);
      
     	function alertContents() {
	    	if (httpRequest.readyState === XMLHttpRequest.DONE) {
    			if (httpRequest.status === 200) {
      			const response = JSON.parse(httpRequest.responseText);
      			alert(response.computedString);
    			} else {
      			alert('There was a problem with the request.');
    			}
  			}
		} 
	}
</script>
</head>
<body>
	<label>Your name:<input type="text" id="ajaxTextbox" /></label>
	<span id="ajaxButton" style="cursor: pointer; text-decoration: underline">
      Make a request</span>
</body>
</html>
//test.php
$name = $_POST['userName'] ?? 'no name';
$computedString = "Hi, " . $name . "!";
$array = ['userName' => $name, 'computedString' => $computedString];
echo json_encode($array);

โœ” Ajax์„ ์‚ฌ์šฉํ•˜๋Š” 3๊ฐ€์ง€ ๋ฐฉ์‹

1.์˜›๋‚  JS ๋ฐฉ์‹

	const ajax = new XMLHttpRequest();
	ajax.onreadystatechange = function(){
      if ((httpRequest.readyState == 4 && httpRequest.status === 200) {
        //์„œ๋ฒ„์—์„œ ๋ฐ›์€ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณณ
      }
  	}
  	ajax.open("GET",:"url์ฃผ์†Œ",true;
  	ajax.send()
  1. ์š”์ฆ˜ JS ๋ฐฉ์‹
	fetch("url์ฃผ์†Œ");
		.then(response) =>{	//์ฝœ๋ฐฑํ•จ์ˆ˜
          if(!response.ok){throw new Error('400์•„๋‹ˆ๋ฉด 500 ์—๋Ÿฌ๋‚จ')}
          return response.json()
        })
        .then((๊ฒฐ๊ณผ) =>{
          //๊ฒฐ๊ณผ ์ฒ˜๋ฆฌํ•˜๋Š”๊ณณ
        })
		.catch() =>	{
          //์—๋Ÿฌ ๋ฐœ์ƒ์‹œ ์ฝ”๋“œ
        }

	function response(){
    ...
    }
  1. ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฐฉ์‹
  • jQuery๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. $.ajax();
  • react๋‚˜ ๋ทฐํ™˜๊ฒฝ์—์„œ axios ์„ค์น˜ํ•ด์„œ ์”€

MDN Ajax
MDN HTTP response status codes
์ฝ”๋”ฉ์• ํ”Œ

0๊ฐœ์˜ ๋Œ“๊ธ€