[JavaScript] AJAX(Asynchronous JavaScript And XML)
1. AJAX (Asynchronous JavaScript And XML)
(1): ํน์ง
๋น๋๊ธฐ ์๋ฐ์คํฌ๋ฆฝํธ
๋น๋๊ธฐ์ ์๋ฐ์คํฌ๋ฆฝํธ ๋์์ ํ๋ ๊ธฐ์
์๋ฒ๋ก๋ถํฐ์ ์๋ต(response) ํ์ธ
XMLHttpRequest`ย ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋งํจ
ํ์ด์ง์ ์๋ก๊ณ ์นจ ์์ด๋ URL์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์.
(2): ํน์ง
- ํ์ด์ง ์๋ก๊ณ ์นจ ์์ด ์๋ฒ์ ์์ฒญ
- ์๋ฒ๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ๋ฐ๊ณ ์์ ์ ์ํ
1) XMLHttpRequest
[XMLHttpRequest - Web API | MDN](https://developer.mozilla.org/ko/docs/Web/API/XMLHttpRequest) |
ํน์ง
- ์ด๊ธฐ ๊ธฐํ๋๋ถํฐ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํ์ง ์์ ๋ธ๋ผ์ฐ์ ๊ฐ์ ๋ถ์ผ์น๊ฐ ๋ฐ์
- IE ์ง์.
(1) XMLHttpRequest ํ์ฉ
- DirectoryHTML ๋ฌธ์์ ๊ฐ์ ๋ฆฌ์์ค๋ค์ ๊ฐ์ ธ์ฌ ์ ์๋๋ก ํด์ฃผ๋ ย ํ๋กํ ์ฝHTML ๋ฌธ์์ ๊ฐ์ ๋ฆฌ์์ค๋ค์ ๊ฐ์ ธ์ฌ ์ ์๋๋ก ํด์ฃผ๋ ย ํ๋กํ ์ฝ
- hello.html
<!-- hello.html -->
<h1 class="text">์๋
ํ์ธ์</h1>
<h2 class="text">Ajax ์
๋๋ค.</h2>
- index.html (๋ฌธ์ ๋ถ๋ฌ์ค๊ธฐ๋ฅผ ํด๋ฆญํ์๋ ์์ฒญ)
<!-- index.html -->
**<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<link rel="stylesheet" href="css/reset.css" />
<style>
body {
margin: 30px;
}
.box {
width: 400px;
padding: 20px;
border: 1px solid #000;
margin-top: 20px;
}
.box h1 {
font-size: 30px;
color: red;
}
.box h2 {
font-size: 25px;
color: green;
}
.box p {
font-size: 14px;
color: #999;
}
</style>
</head>
<body>
<button class="btn">๋ฌธ์๋ถ๋ฌ์ค๊ธฐ</button>
<div class="box">
<p>๋ฐ์ดํฐ๊ฐ ๋ค์ด์ด</p>
</div>
<script>
const btn = document.querySelector(".btn");
const box = document.querySelector(".box");
btn.addEventListener("click", (e) => {
e.preventDefault();
const xhr = new XMLHttpRequest();
const method = "GET";
const url = "backend/hello.html";
xhr.onreadystatechange = (e) => {
console.log(e);
const { target } = e;
if ((target.readyState = XMLHttpRequest.DONE)) {
//Ajax ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํํ๋ ์์น
if (target.status == 200) {
alert("dd");
const req = target.responseText;
box.insertAdjacentHTML("beforeend", req);
} else {
const a = parseInt(target.status / 100);
if (a == 4) {
console.log(
"์์ฒญ ์ฃผ์๊ฐ ์๋ชป๋์์ต๋๋ค",
target.status,
target.statusText
);
} else if (a == 5) {
console.log(
"์๋ฒ ์๋ต์ด ์์",
target.status,
target.statusText
);
} else {
console.log("์์ฒญ์ ์คํจ", target.status, target.statusText);
}
}
}
};
xhr.open(method, url);
xhr.send();
});
</script>
</body>
</html>
**
- ์์ฒญ ๊ฒฐ๊ณผ๊ฐ
2) Fetch
[Fetch API - Web API | MDN](https://developer.mozilla.org/ko/docs/Web/API/Fetch_API) |
ํน์ง
- promise ๊ธฐ๋ฐ
- ์ด๋ฏธ ์ ๋ช ์ธ๋ API๊ฐ ์ ๊ณต๋จ์ผ๋ก์จ ๋ธ๋ผ์ฐ์ ๊ฐ์ ๋ถ์ผ์น๊ฐ ์์
- IE ๋๋ถ๋ถ์์ ๋์ํ์ง ์์
(1) Fetch ํ์ฉ
- Directory
- hello.html
<!-- hello.html -->
<h1 class="text">์๋
ํ์ธ์</h1>
<h2 class="text">Ajax ์
๋๋ค.</h2>
- index.html (๋ฌธ์ ๋ถ๋ฌ์ค๊ธฐ๋ฅผ ํด๋ฆญํ์๋ ์์ฒญ)
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<link rel="stylesheet" href="css/reset.css" />
<style>
body {
margin: 30px;
}
.box {
width: 400px;
padding: 20px;
border: 1px solid #000;
margin-top: 20px;
}
.box h1 {
font-size: 30px;
color: red;
}
.box h2 {
font-size: 25px;
color: green;
}
.box p {
font-size: 14px;
color: #999;
}
</style>
</head>
<body>
<button class="btn">๋ฌธ์๋ถ๋ฌ์ค๊ธฐ</button>
<div class="box">
<p>๋ฐ์ดํฐ๊ฐ ๋ค์ด์ด</p>
</div>
<script>
const btn = document.querySelector(".btn");
const box = document.querySelector(".box");
btn.addEventListener("click", (e) => {
const url = "backend/hello.html";
fetch(url).then((res) => {
console.log(res);
res.text().then((text) => {
box.insertAdjacentHTML("beforeend", text);
});
});
});
</script>
</body>
</html>
- ์์ฒญ ๊ฒฐ๊ณผ๊ฐ
3) Axios
[์์ํ๊ธฐ | ย Axios Docs](https://axios-http.com/kr/docs/intro) |
ํน์ง
- ๋ธ๋ผ์ฐ์ ๋ฅผ ์ํดย XMLHttpRequestsย ์์ฑ
- node.js๋ฅผ ์ํดย httpย ์์ฒญ ์์ฑ
- Promiseย API๋ฅผ ์ง์
- ์์ฒญ ๋ฐ ์๋ต ์ธํฐ์ ํธ
- ์์ฒญ ๋ฐ ์๋ต ๋ฐ์ดํฐ ๋ณํ
- ์์ฒญ ์ทจ์
- JSON ๋ฐ์ดํฐ ์๋ ๋ณํ
- XSRF๋ฅผ ๋ง๊ธฐ์ํ ํด๋ผ์ด์ธํธ ์ฌ์ด๋ ์ง์
- ํฌ๋ก์ค ๋ธ๋ผ์ฐ์ง์ ์ ๊ฒฝ์ ๋ง์ด์ผ๊ธฐ์ ๋ธ๋ผ์ฐ์ ํธํ์ฑ์ด ์ข๋ค
- IE ์ง์.
Axios ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ณต์ ๋ฌธ์
์ค์น
npm ์ฌ์ฉํ๊ธฐ:
$npm install axios
bower ์ฌ์ฉํ๊ธฐ:
$ bower install axios
yarn ์ฌ์ฉํ๊ธฐ:
$yarn add axios
jsDelivr CDN ์ฌ์ฉํ๊ธฐ:
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
unpkg CDN ์ฌ์ฉํ๊ธฐ:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
(1) Axios ํ์ฉ
- Directory
- hello.html
<!-- hello.html -->
<h1 class="text">์๋
ํ์ธ์</h1>
<h2 class="text">Ajax ์
๋๋ค.</h2>
- index.html (๋ฌธ์ ๋ถ๋ฌ์ค๊ธฐ๋ฅผ ํด๋ฆญํ์๋ ์์ฒญ)
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<link rel="stylesheet" href="css/reset.css" />
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<style>
body {
margin: 30px;
}
.box {
width: 400px;
padding: 20px;
border: 1px solid #000;
margin-top: 20px;
}
.box h1 {
font-size: 30px;
color: red;
}
.box h2 {
font-size: 25px;
color: green;
}
.box p {
font-size: 14px;
color: #999;
}
</style>
</head>
<body>
<button class="btn">๋ฌธ์๋ถ๋ฌ์ค๊ธฐ</button>
<div class="box">
<p>๋ฐ์ดํฐ๊ฐ ๋ค์ด์ด</p>
</div>
<script>
const btn = document.querySelector(".btn");
const box = document.querySelector(".box");
btn.addEventListener("click", (e) => {
///xxx.data: ์ ๋ณด๊ฐ ๋ค์ด๊ฐ ์์ jsonํ์ผ์ ๋ถ๋ฌ์ค๋ฉด => ๊ฐ์ฒด๋ก ๊ฐ์ ธ์ด
const url = "backend/hello.html";
// axios.get(url).then(res=>console.log(res.data))
axios
.get(url)
.then((res) => {
console.log(res);
box.insertAdjacentHTML("beforeend", res.data);
})
.catch((err) => {
console.log("ํ์ผ์ฐ๊ฒฐ ์๋ฌ");
console.log(err.res.status);
console.log(err.res.data);
})
.finally(() => {
console.log("๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ ์๋ฃ");
});
});
</script>
</body>
</html>
- ์์ฒญ ๊ฒฐ๊ณผ๊ฐ