TEXT. HTML ํ์ผ ๊ฐ์ ธ์ค๊ธฐ load()
์ ์ด์ฟผ๋ฆฌํ์ผ๊ฐ์ ธ์ค๊ธฐ.html ์์ฑ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- ์ ์ด์ฟผ๋ฆฌCDN -->
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"
></script>
</head>
<body>
<div class="container">
<div id="result"></div>
</div>
<script>
$("#result").load("text.html");
</script>
</body>
</html>
text.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>๊ฐ์ ธ๊ฐ๋ฌธ์</title>
</head>
<body>
<h1>html ๋ฌธ์์
๋๋ค.</h1>
</body>
</html>
ํ๋ฉด ์ถ๋ ฅ

html,txt ๋ฑ ํ์ผ์ load()๋ฅผ ํตํด ๊ฐ์ ธ์ฌ ์ ์์
๋ง์ฝ ์๋ชป๋ ํ์ผ ( ์๋ ํ์ผ) ์ ๊ฐ์ ธ์ค๋ ค๊ณ ์๋ ํ์ ๊ฒฝ์ฐ ์ค๋ฅ ๋ฐ์ํ๋๋ก ์ฝ๋ ์์ฑํ ์ ์์.
<script>
$("#result").load("text.html", function (Response, status, xhr) {
if (status === "success") {
alert("์ด์์์");
} else if (status === "error") {
alert("์๋ฌ: " + xhr.status);
}
});
</script>
์ํ๊ฐ ์ฑ๊ณต์ด๋ฉด ์ด์์์ alert์ฐฝ ์ถ๋ ฅ
์๋ฌ๋ฉด ์๋ฌ ๋ด์ฉ alert์ฐฝ ์ถ๋ ฅ
๊ทธ๋์ ๋ง์ฝ text.html ์ด ์๋ text11.html (์๋ ํ์ผ) ์ ๊ฐ์ ธ์จ๋ค๊ณ ์๋ํ๋ฉด

์์ ๊ฐ์ alert์ฐฝ์ด ๋ฐ์ํ๋ค.
JSON ํ์ ๊ฐ์ ธ์ค๊ธฐ
์์ ์ ์ด์ฟผ๋ฆฌํ์ผ๊ฐ์ ธ์ค๊ธฐ.html ๋ณต์ฌํด์ ์ ์ด์ฟผ๋ฆฌJSON๊ฐ์ ธ์ค๊ธฐ.html ์์ฑ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- ์ ์ด์ฟผ๋ฆฌCDN -->
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"
></script>
</head>
<body>
<div class="container">
<div id="result"></div>
</div>
<script>
$.getJSON("user.json", function (data) {
console.log(data);
});
</script>
</body>
</html>
๊ฐ์ ธ์ฌ
user.json ์์ฑ
[
{
"id": 1,
"first_name": "Jeanette",
"last_name": "Penddreth",
"email": "jpenddreth0@census.gov",
"gender": "Female",
"ip_address": "26.58.193.2"
},
{
"id": 2,
"first_name": "Giavani",
"last_name": "Frediani",
"email": "gfrediani1@senate.gov",
"gender": "Male",
"ip_address": "229.179.4.212"
},
{
"id": 3,
"first_name": "Noell",
"last_name": "Bea",
"email": "nbea2@imageshack.us",
"gender": "Female",
"ip_address": "180.66.162.255"
},
{
"id": 4,
"first_name": "Willard",
"last_name": "Valek",
"email": "wvalek3@vk.com",
"gender": "Male",
"ip_address": "67.76.188.26"
}
]
๋ฐฐ์ด์์ 4๊ฐ์ ๊ฐ์ฒด๊ฐ ๋ค์ด๊ฐ์์.

getJSON์ ์ฌ์ฉํ๋ฉด JSON ํ์ ์ ๊ฐ์ ธ์ฌ ์ ์์.
์ด ๋ฐฐ์ด๋ค์ ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํด์ ํ๋ฉด์ ์ถ๋ ฅํ๊ธฐ\
$.getJSON("user.json", function (data) {
//console.log(data);
//ํ๋ฉด์ ์ถ๋ ฅํ๊ธฐ (๋ฐ๋ณต๋ฌธ์ผ๋ก ๊ฐ์ ธ์ด)
$.each(data, function (i, user) {
console.log(i, user);
$("ul#users").append(`<li>${user.first_name}</li>`); //๋ฒกํฑ ๋ฐ์ดํ ์จ์ผํจ
});
});

AJAX ๋ก JSON ํ์ ๊ฐ์ ธ์ค๊ธฐ
https://jsonplaceholder.typicode.com/
JSONPlaceholder - Free Fake REST API
{JSON} Placeholder Free fake API for testing and prototyping. Powered by JSON Server + LowDB. Tested with XV. Serving ~2 billion requests each month.
jsonplaceholder.typicode.com

https://jsonplaceholder.typicode.com/posts
posts ํด๋ฆญ

์ฌ๊ธฐ์ ์๋ ๋ด์ฉ๋ค์ ๋ค๊ณ ์ฌ๊ฑฐ์
์ ์ด์ฟผ๋ฆฌ JSON๊ฐ์ ธ์ค๊ธฐ.html ๋ณต์ฌํด์ ์ ์ด์ฟผ๋ฆฌAJAX.html ์์ฑ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- ์ ์ด์ฟผ๋ฆฌCDN -->
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"
></script>
</head>
<body>
<div class="container">
<div id="result"></div>
</div>
<script>
$.ajax({
url: "https://jsonplaceholder.typicode.com/posts", //์๋ฒ์ URL์ฃผ์
method: "GET", // HTTP ์์ฒญ ๋ฐฉ์(GET,POST)
dataType: "json", // ์๋ฒ์์ ๋ณด๋ด์ค ๋ฐ์ดํฐ์ ํ์
}).done(function (data) {
// .done ์์ ์๋ฃ๋ฅผ ๋ฐ๋๊ฒ ์ฑ๊ณตํ์ ๊ฒฝ์ฐ
console.log(data); //์ฑ๊ณต ์ ๋ฐ์ดํฐ ์ฝ์๋ก ์ถ๋ ฅ
$.each(data, function (i, item) {
// ๋ฐ๋ณต๋ฌธ ์ฌ์ฉํด์ ํ๋ฉด์ถ๋ ฅ
$("#result").append(`<h3>${item.title}</h3><p>${item.body}</p>`);
});
});
</script>
</body>
</html>
์ฝ์์ 100๊ฐ ๊ฐ์ฒด ๋ค์ด์จ๊ฑฐ ํ์ธ๊ฐ๋ฅํ๊ณ

ํ๋ฉด ์ถ๋ ฅ๋ ์ ์์ ์ผ๋ก ๋ ๊ฒ์ด ํ์ธ๋๋ค.

'FRONTEND > ๊ธฐํ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| AJAX ๊ธฐ๋ณธ ๊ฐ๋ ๋ฐ ์์ (1) | 2023.10.16 |
|---|---|
| ๋ก๋ ๋ฒํธ ๊ฐ์ ธ์ค๊ธฐ (0) | 2023.10.06 |
| Dom ์์ฉ์ค์ต (1) | 2023.10.05 |
| jQuery - event (0) | 2023.10.05 |
| jQuery ? (1) | 2023.10.05 |