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๋จ๊ณ๋ฅผ ์๋ตํ ์์์ด์ ์์ ๋๋ ๊ฒ์ด ๋ ํจ์จ์ ์ด๋ผ๊ณ ํฉ๋๋ค.
<%@ 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 + "์
๋๋ค.");
%>
<%@ 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);
1.์๋ JS ๋ฐฉ์
const ajax = new XMLHttpRequest();
ajax.onreadystatechange = function(){
if ((httpRequest.readyState == 4 && httpRequest.status === 200) {
//์๋ฒ์์ ๋ฐ์ ๋ฐ์ดํฐ ์ฒ๋ฆฌํ๋ ๊ณณ
}
}
ajax.open("GET",:"url์ฃผ์",true;
ajax.send()
fetch("url์ฃผ์");
.then(response) =>{ //์ฝ๋ฐฑํจ์
if(!response.ok){throw new Error('400์๋๋ฉด 500 ์๋ฌ๋จ')}
return response.json()
})
.then((๊ฒฐ๊ณผ) =>{
//๊ฒฐ๊ณผ ์ฒ๋ฆฌํ๋๊ณณ
})
.catch() => {
//์๋ฌ ๋ฐ์์ ์ฝ๋
}
function response(){
...
}