Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Spring
- ์์์ค
- ์ฐ์ ์ฉ์ ๊ธฐ์๊ธ
- ํ์๊ด
- ์ ๊ธฐ
- AWS
- ๋์ปค
- java
- ํ์ด์ฌ
- docker
- EC2
- ์์ด์ญ์ค
- ๋จธ์ ๋ฌ๋
- Kepco
- ์ ๊ธฐ์๊ธ๊ณ์ฐ
- ์์ธก
- ํ๊ตญ์ ๋ ฅ๊ณต์ฌ
- python
- ๋ฐ์ ๋
- Machine Learning
- ์ฐ์ ์ฉ
- ํ๊ตญ์ ๋ ฅ
- crawling
- ํ์
- ์ ๊ธฐ์๊ธ
- ํฌ๋กค๋ง
- ์ ๋ ฅ๊ณํต
- ์ ๋ ฅ
- ๊ทธ๋ํ
- ์๋ฐ
Archives
- Today
- Total
gyeomii
AJAX(1) ๋ณธ๋ฌธ
๋ฐ์ํ
๐ AJAX Test
<h4>ajax๋ฅผ ์ด์ฉํ ๋ฐ์ดํฐ ์ ์ก</h4>
<button type="button">ํ์ธ</button>
<div></div
$('button:button').on('click', function() {
let request = new XMLHttpRequest();
request.open('get', 'newTest.jsp', true);
request.send();
request.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
let a = JSON.parse(this.responseText);
let result = "<table>";
result += "<tr>";
result += "<th>์ด๋ฆ</th>";
result += "<th>์ฃผ์</th>";
result += "<th>๋ฒํธ</th>";
result += "</tr>";
$.each(a,function(){
result += "<tr>";
result += "<td>" + this.name + "</td>";
result += "<td>" + this.addr + "</td>";
result += "<td>" + this.tel + "</td>";
result += "</tr>";
});
result += "</table>";
$('div').html(result);
}
};
});
- ajax๋ฅผ ์ฌ์ฉํ๋ฉด ๊ธฐ์กด ํ์ด์ง์ ์ ๋ณด๋ฅผ ์ ๋ฐ์ดํธํ๊ณ ์ถ๋ ฅํ ์ ์๋ค.
โ ์๋ฒ์ ์ ๋ณด๋ฅผ ์์ฒญํ๊ธฐ ์ํด XMLHttpRequest๊ฐ์ฒด๋ฅผ ์์ฑํด์ผํ๋ค.
let request = new XMLHttpRequest();
- XMLHttpRequest๊ฐ์ฒด :
- ์๋ฒ์ ์ํธ์์ฉํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค.
- ์๋ก๊ณ ์นจ ์์ด ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๋ฉฐ ์ฃผ๋ก ajax์ ์ฌ์ฉ๋๋ค.
โ ๊ฐ์ฒด๋ฅผ ์์ฑํ ํ ์์ฒญ์ ๋ณด๋ฅผ ์ค์ ํ๊ณ ์๋ฒ๋ก ์ ๋ฌํด์ผํ๋ค.
request.open('get', 'newTest.jsp', true);
request.send();
- request.open(method ๋ฐฉ์, url, async)
- method : get๋ฐฉ์์ธ๊ฐ post๋ฐฉ์์ธ๊ฐ
- url : ์์ฒญํ ํ์ด์ง
- async : ๋๊ธฐ์ ์ฒ๋ฆฌ์ธ์ง ๋น๋๊ธฐ์ ์ฒ๋ฆฌ์ธ์ง ์ค์
- true : ๋น๋๊ธฐ / false : ๋๊ธฐ
- ๋๊ธฐ์ ์ฒ๋ฆฌ
- ์๋ฒ์ ํด๋ผ์ด์ธํธ๊ฐ ๋์์ ํต์ ํ์ฌ ํ๋ก์ธ์ค ์ํ, ์ข ๋ฃ๋ฅผ ํจ๊ป ์งํํ๋ค.
- ์๋ก์ด ์ฝํ ์ธ ๋ฅผ ์ถ๊ฐ, ๋ถ๋ฌ์ฌ๋ ํ์ด์ง๋ฅผ ๋ฆฌ๋ก๋ํ๊ฑฐ๋ ์ด๋ํด์ผ ํ๋ค.
- ๋น๋๊ธฐ์ ์ฒ๋ฆฌ
- ํ๋ก์ธ์ค๊ฐ ์ข ๋ฃ๋์ด๋ ์ธ์ ๋ ์ง ํ์ํ ์ ๋ณด๋ฅผ ์์ฒญํ๊ณ ๋ก๋ํ ์ ์๋ค.
- ํ์ด์ง๋ฅผ ์๋ก๊ณ ์นจํ ํ์ ์๋ค.
- ์ ๋ ฅํ ์์ ๋ฐ ์ ๋ณด๊ฐ ๊ทธ๋๋ ์ ์ง๋๋ค.
- ํ์ด์ง์์ ํ์ํ ๋ถ๋ถ๋ง ํธ์ถํ๋ฏ๋ก ๋ถํ์ํ ํธ๋ํฝ์ ์ค์ผ ์ ์๋ค.
- ์ฝ๋๊ฐ ๋ณต์กํด์ง๊ณ ๋๋ฒ๊น ํ๊ธฐ ์ด๋ ค์ด ๋ฌธ์ ๊ฐ ์๋ค.
- try-catch๊ตฌ๋ฌธ ์ ์ฉ์ด ์ด๋ ต๋ค.
- but promise ๊ตฌ๋ฌธ์ ์ฌ์ฉํ๋ฉด ๋น๋๊ธฐ์์ ๋๊ธฐ์์ฒ๋ผ ์ฌ์ฉํ ์ ์์ด์ try-catch์ ์ฉ์ด ๊ฐ๋ฅํด์ง๋ค.
- request.send() : ์์ฒญ ์ ๋ณด ์๋ฒ๋ก ์ ๋ฌ
โ ์๋ฒ๋ก ๋ถํฐ ์๋ต ๋ฐ์ดํฐ ๋ฐ๊ธฐ
request.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
let a = JSON.parse(this.responseText);
let result = "<table>";
result += "<tr>";
result += "<th>์ด๋ฆ</th>";
result += "<th>์ฃผ์</th>";
result += "<th>๋ฒํธ</th>";
result += "</tr>";
$.each(a,function(){
result += "<tr>";
result += "<td>" + this.name + "</td>";
result += "<td>" + this.addr + "</td>";
result += "<td>" + this.tel + "</td>";
result += "</tr>";
});
result += "</table>";
$('div').html(result);
}
};
- request.onreadystatechange = function(){}
- XMLHttpRequest๊ฐ์ฒด์ readyState๊ฐ์ด ๋ณํ ๋๋ง๋ค ์๋์ผ๋ก ํธ์ถ๋๋ ํจ์๋ฅผ ์ค์ ํ๋ค.
- XMLHttpRequest๊ฐ์ฒด๋ช .onreadystatechange = ํธ์ถ๋๋ ํจ์(){๋ด์ฉ}
- ์ด ์์ฑ์ ์ฌ์ฉํด์ ์๋ฒ์ ์์ฒญํ ๋ฐ์ดํฐ์ ์กด์ฌ ๋ฐ ์๋ต ๋์ฐฉ์ ์๊ฐ์ ํน์ ํ๋ค.
- XMLHttpRequest๊ฐ์ฒด์ readyState๊ฐ์ด ๋ณํ ๋๋ง๋ค ์๋์ผ๋ก ํธ์ถ๋๋ ํจ์๋ฅผ ์ค์ ํ๋ค.
- readyState ํ๋กํผํฐ
- readyState ํ๋กํผํฐ๋ XMLHttpRequest ๊ฐ์ฒด์ ํ์ฌ ์ํ๋ฅผ ๋ํ๋ธ๋ค.
- ์ด ํ๋กํผํฐ์ ๊ฐ์ ๊ฐ์ฒด์ ํ์ฌ ์ํ์ ๋ฐ๋ผ ๋ค์๊ณผ ๊ฐ์ ์ฃผ๊ธฐ๋ก ๋ณํํ๋ค.
์ํ ์ฝ๋ ๊ฐ ์ค๋ช UNSENT 0 XMLHttpRequest ๊ฐ์ฒด๊ฐ ์์ฑ๋จ. OPEND 1 open() ๋ฉ์๋๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์คํ๋จ. HEADERS_RECEIVED 2 ๋ชจ๋ ์์ฒญ์ ๋ํ ์๋ต์ด ๋์ฐฉํจ. LOADING 3 ์์ฒญํ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌ ์ค์. DONE 4 ์์ฒญํ ๋ฐ์ดํฐ์ ์ฒ๋ฆฌ๊ฐ ์๋ฃ๋์ด ์๋ตํ ์ค๋น๊ฐ ์๋ฃ๋จ.
- status ํ๋กํผํฐ
- status ํ๋กํผํฐ๋ ์๋ฒ์ ๋ฌธ์ ์ํ๋ฅผ ๋ํ๋๋ค.
์ํ ์ฝ๋ ์ค๋ช 200 ์๋ฒ์ ๋ฌธ์๊ฐ ์กด์ฌ, ์ ์์ ์ผ๋ก ์๋ต์ ๋ฐ์ 403 ์์ฒญ ์ค๋ฅ 404 ์๋ฒ์ ๋ฌธ์๊ฐ ์กด์ฌํ์ง ์์, ํ์ด์ง๋ฅผ ์ฐพ์ง ๋ชปํจ 500 ์๋ฒ์๋ฌ
- status ํ๋กํผํฐ๋ ์๋ฒ์ ๋ฌธ์ ์ํ๋ฅผ ๋ํ๋๋ค.
โ JSON๋ฐ์ดํฐ ํ์ฑํ๊ธฐ
let a = JSON.parse(this.responseText);
$.each(a,function(){
let result = "<table>";
result += "<tr>";
result += "<td>" + this.name + "</td>";
result += "<td>" + this.addr + "</td>";
result += "<td>" + this.tel + "</td>";
result += "</tr>";
- newTest.jspํ์ผ ์ ๋ค์ด์๋ json object array ๋ฐ์ดํฐ
[
{"name" : "์ฅ์ฌํ", "addr" : "๋์ ", "tel" : "010-1111-1111"},
{"name" : "์ค์ฌ์ด", "addr" : "๋์ ", "tel" : "010-2222-2222"},
{"name" : "๊น๋ฏธ์ ", "addr" : "๋์ ", "tel" : "010-3333-3333"},
{"name" : "๊น์ฑ๊ฒธ", "addr" : "๋์ ", "tel" : "010-1234-1234"}
]
- JSON Object Array๋ฐ์ดํฐ๋ฅผ JavaScript์์ ์ฌ์ฉํ ์ ์๋๋ก ํ์ฑํ๋ ์์
- { key : value } ํํ๋ก ์ ์ฅ๋ JSON๋ฐ์ดํฐ๋ฅผ key๋ก ์ ๊ทผํด ๋ฐ์ดํฐ๋ฅผ ๊บผ๋ด์จ๋ค.
- $.each(๋ฐฐ์ด , function(index, value){ result = this.key๊ฐ });
- JSON ๊ฐ์ฒด๊ฐ ๋ฐฐ์ด๋ก ์กด์ฌํ ๋ ์ฌ๋ฌ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๊บผ๋ด์ค๋ ค๋ฉด each๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
- each๋ฅผ ์ฌ์ฉํ๋ฉด for, while์ฒ๋ผ ๋ฐฐ์ด์ ์ํํ๋ฉฐ ๋ฐ์ดํฐ๋ฅผ ๊บผ๋ด์จ๋ค.
๋ฐ์ํ
'๊ฐ๋ฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JavaScript ์์ (0) | 2023.07.04 |
---|---|
AJAX(2) - JSP์ ์ฐ๊ณ (0) | 2023.07.04 |
MVC ํจํด (0) | 2023.06.26 |
[Python] JSON, CSV ์ฝ๊ณ ์ฐ๊ธฐ (0) | 2023.06.16 |
Dockerfile & Docker-compose (0) | 2023.06.08 |