Language/JavaScript (WEB)

๐ŸŒ XMLHttpRequest ์œผ๋กœ AJAX ์š”์ฒญํ•˜๊ธฐ

์ธํŒŒ_ 2021. 9. 21. 22:20

XMLHttpRequest

AJAX (Asynchronous Javascript And XML)

Ajax๋Š” ์›น ํŽ˜์ด์ง€ ์ „์ฒด๋ฅผ ๋‹ค์‹œ ๋กœ๋”ฉํ•˜์ง€ ์•Š๊ณ ๋„, ์›น ํŽ˜์ด์ง€์˜ ์ผ๋ถ€๋ถ„๋งŒ์„ ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค. Ajax๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์˜์—ญ์—์„œ ์„œ๋ฒ„์™€ ํ†ต์‹ ํ•˜์—ฌ, ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์›น ํŽ˜์ด์ง€์˜ ์ผ๋ถ€๋ถ„์—๋งŒ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค.

ajax๋ฅผ ํ†ตํ•œ ์›น ๋ธŒ๋ผ์šฐ์ €์™€ ์›น ์„œ๋ฒ„ ๊ฐ„์˜ ํ†ต์‹  ์ ˆ์ฐจ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

XMLHttpRequest

  1. ์‚ฌ์šฉ์ž์— ์˜ํ•œ ์š”์ฒญ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒ
  2. ์š”์ฒญ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์— ์˜ํ•ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ํ˜ธ์ถœ
  3. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” XMLHttpRequest ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋ฒ„๋กœ ์š”์ฒญ์„ ๋ณด๋ƒ„
  4. ์„œ๋ฒ„๋Š” ์ „๋‹ฌ๋ฐ›์€ XMLHttpRequest ๊ฐ์ฒด๋ฅผ ๊ฐ€์ง€๊ณ  ์š”์ฒญ์„ ์ฒ˜๋ฆฌ
  5. ์„œ๋ฒ„๋Š” ์ฒ˜๋ฆฌํ•œ ๊ฒฐ๊ณผ๋ฅผ HTML, XML ๋˜๋Š” JSON ํ˜•ํƒœ์˜ ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑ ์›น ๋ธŒ๋ผ์šฐ์ €์— ์ „๋‹ฌ
  6. ์ด๋•Œ ์ „๋‹ฌ๋˜๋Š” ์‘๋‹ต์€ ์ƒˆ๋กœ์šด ํŽ˜์ด์ง€๋ฅผ ์ „๋ถ€ ๋ณด๋‚ด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ์„ ์ „๋‹ฌํ•œ๋‹ค.
  7. ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์›น ํŽ˜์ด์ง€์˜ ์ผ๋ถ€๋ถ„๋งŒ์„ ๊ฐฑ์‹ ํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ˜ธ์ถœ. ๊ฒฐ๊ณผ์ ์œผ๋กœ ์›น ํŽ˜์ด์ง€์˜ ์ผ๋ถ€๋ถ„๋งŒ์ด ๋‹ค์‹œ ๋กœ๋”ฉ๋˜์–ด ํ‘œ์‹œ๋œ๋‹ค.

XMLHttpRequest ๊ฐ์ฒด

XMLHttpRequest ๊ฐ์ฒด๋Š” ์›น ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์„œ๋ฒ„์™€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตํ™˜ํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค. ์›น ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ๊ณ„์†ํ•ด์„œ ์„œ๋ฒ„์™€ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ๋ฐ”๋กœ ์ด ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

// 1. XMLHttpRequest๊ฐ์ฒด ์ƒ์„ฑ
var httpRequest = new XMLHttpRequest(); 

// 2. onreadystatechange ๋“ฑ๋ก
httpRequest.onreadystatechange = function() {
	// XMLXttpRequest ๊ฐ์ฒด์˜ ํ˜„์žฌ ์ƒํƒœ์™€ ์„œ๋ฒ„ ์ƒ์˜ ๋ฌธ์„œ ์กด์žฌ ์œ ๋ฌด๋ฅผ ํ™•์ธ
    if (httpRequest.readyState == XMLHttpRequest.DONE && httpRequest.status == 200 ) {
    	console.log(httpRequest.responseText); // ์„œ๋ฒ„์— ์š”์ฒญํ•˜์—ฌ ์‘๋‹ต์œผ๋กœ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜
    }
};

// 3. GET ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋™์‹œ์— ์ „๋‹ฌํ•จ
httpRequest.open("GET", "์„œ๋ฒ„URL", true);
httpRequest.send();

 

XMLHttpRequest ์š”์ฒญ ๋ณด๋‚ด๊ธฐ

XMLHttpRequest ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ• ์„ค๋ช…์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

1. XMLHttpRequest ๊ฐ์ฒด ์ƒ์„ฑ

var httpRequest = new XMLHttpRequest();

 

2. onreadystatechange ๋“ฑ๋ก

onreadystatechange๋กœ ์„œ๋ฒ„๋กœ ๋ถ€ํ„ฐ ์‘๋‹ต์ด ์˜ค๊ฒŒ ๋˜์–ด XMLHttpRequest ๊ฐ์ฒด์˜ ๊ฐ’์ด ๋ณ€ํ•˜๊ฒŒ ๋˜๋ฉด ์ด๋ฅผ ๊ฐ์ง€ํ•ด ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ์„ค์ •ํ•œ๋‹ค. ํ•จ์ˆ˜๋ฅผ ๋“ฑ๋กํ•˜๊ฒŒ ๋˜๋ฉด ์„œ๋ฒ„์— ์š”์ฒญํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜๊ณ , ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์‘๋‹ต์ด ๋„์ฐฉํ•˜๋Š” ์ˆœ๊ฐ„์„ ํŠน์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

// 2. onreadystatechange ๋“ฑ๋ก
httpRequest.onreadystatechange = function() {

};

 

3. ์„œ๋ฒ„๋กœ ๋ณด๋‚ผ Ajax ์š”์ฒญ์˜ ํ˜•์‹์„ ์„ค์ •

httpRequest.open(์ „๋‹ฌ๋ฐฉ์‹, URL์ฃผ์†Œ, ๋™๊ธฐ์—ฌ๋ถ€) ๋ฉ”์†Œ๋“œ๋ฅผ ๋“ฑ๋กํ•œ๋‹ค.

  • ์ „๋‹ฌ ๋ฐฉ์‹์€ ์š”์ฒญ์„ ์ „๋‹ฌํ•  ๋ฐฉ์‹์œผ๋กœ GET ๋ฐฉ์‹๊ณผ POST ๋ฐฉ์‹ ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค.
  • URL ์ฃผ์†Œ๋Š” ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ์„œ๋ฒ„์˜ ํŒŒ์ผ ์ฃผ์†Œ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.
  • ๋™๊ธฐ ์—ฌ๋ถ€๋Š” ์š”์ฒญ์„ ๋™๊ธฐ์‹์œผ๋กœ ์ „๋‹ฌํ• ์ง€ ๋น„๋™๊ธฐ์‹์œผ๋กœ ์ „๋‹ฌํ• ์ง€๋ฅผ ์ „๋‹ฌํ•œ๋‹ค. (true : ๋น„๋™๊ธฐ / false : ๋™๊ธฐ)
httpRequest.open("GET", "์„œ๋ฒ„URL", true);

 

4. ์ž‘์„ฑ๋œ Ajax ์š”์ฒญ์„ ์„œ๋ฒ„๋กœ ์ „๋‹ฌ

httpRequest.send() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„๋กœ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•œ๋‹ค. ์ด๋•Œ send() ๋ฉ”์„œ๋“œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์“ฐ๋ƒ ์•ˆ์“ฐ๋ƒ์— ๋”ฐ๋ผ GET / POST ๋ฐฉ์‹์œผ๋กœ ๋‹ค๋ฅด๊ฒŒ ๋ณด๋‚ด๊ฒŒ ๋œ๋‹ค.

  • send() - GET ๋ฐฉ์‹
  • send(๋ฌธ์ž์—ด) - POST ๋ฐฉ์‹
httpRequest.send();

 

POST ๋ฐฉ์‹ ์„œ๋ฒ„ ์š”์ฒญ

Ajax์—์„œ๋Š” ์„œ๋ฒ„์— POST ๋ฐฉ์‹์˜ ์š”์ฒญ์„ ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑ ํ•œ๋‹ค. ์ด๋•Œ ์„œ๋ฒ„๋กœ ์ „์†กํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋Š” HTTP ํ—ค๋”์— ํฌํ•จ๋˜์–ด ์ „์†ก๋œ๋‹ค. ๋”ฐ๋ผ์„œ setRequestHeader() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋จผ์ € ํ—ค๋”๋ฅผ ์ž‘์„ฑํ•œ ํ›„์—, send() ๋ฉ”์†Œ๋“œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๋ฉด ๋œ๋‹ค.

var httpRequest = new XMLHttpRequest(); // ๊ฐ์ฒด ์ƒ์„ฑ

httpRequest.onreadystatechange = function() {
    if (httpRequest.readyState == XMLHttpRequest.DONE && httpRequest.status == 200 ) {
        console.log(httpRequest.responseText);
    }
};

// POST ๋ฐฉ์‹์˜ ์š”์ฒญ์€ ๋ฐ์ดํ„ฐ๋ฅผ Http ํ—ค๋”์— ํฌํ•จ์‹œ์ผœ ์ „์†กํ•จ.
httpRequest.open("POST", "/examples/media/request_ajax.php", true);
httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // ๋ณด๋‚ผ ๋ฐ์ดํ„ฐ ํ—ค๋”๋ฅผ ์ง€์ •
httpRequest.send("city=Seoul&zipcode=06141"); // ๋ณด๋‚ผ ๋ฐ์ดํ„ฐ ์ง€์ •

XMLHttpRequest ์‘๋‹ต ๋ฐ›๊ธฐ

 

readyState ํ”„๋กœํผํ‹ฐ

Ajax์—์„œ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ์˜ ์‘๋‹ต์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” XMLHttpRequest ๊ฐ์ฒด์˜ readyState ํ”„๋กœํผํ‹ฐ๋Š” XMLHttpRequest ๊ฐ์ฒด์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์ด ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์€ ๊ฐ์ฒด์˜ ํ˜„์žฌ ์ƒํƒœ์— ๋”ฐ๋ผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฃผ๊ธฐ๋กœ ์ˆœ์„œ๋Œ€๋กœ ๋ณ€ํ™”ํ•œ๋‹ค.

  1. UNSENT (์ˆซ์ž 0) : XMLHttpRequest ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋จ.
  2. OPENED (์ˆซ์ž 1) : open() ๋ฉ”์†Œ๋“œ๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์‹คํ–‰๋จ.
  3. HEADERS_RECEIVED (์ˆซ์ž 2) : ๋ชจ๋“  ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์ด ๋„์ฐฉํ•จ.
  4. LOADING (์ˆซ์ž 3) : ์š”์ฒญํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌ ์ค‘์ž„.
  5. DONE (์ˆซ์ž 4) : ์š”์ฒญํ•œ ๋ฐ์ดํ„ฐ์˜ ์ฒ˜๋ฆฌ๊ฐ€ ์™„๋ฃŒ๋˜์–ด ์‘๋‹ตํ•  ์ค€๋น„๊ฐ€ ์™„๋ฃŒ๋จ.
var httpRequest = new XMLHttpRequest();
var currentState = "";

httpRequest.onreadystatechange = function () {
    switch (httpRequest.readyState) {
        case XMLHttpRequest.UNSET:
            currentState += "XMLHttpRequest ๊ฐ์ฒด์˜ ํ˜„์žฌ ์ƒํƒœ๋Š” UNSET ์ž…๋‹ˆ๋‹ค.<br>";
            break;
        case XMLHttpRequest.OPENED:
            currentState += "XMLHttpRequest ๊ฐ์ฒด์˜ ํ˜„์žฌ ์ƒํƒœ๋Š” OPENED ์ž…๋‹ˆ๋‹ค.<br>";
            break;
        case XMLHttpRequest.HEADERS_RECEIVED:
            currentState += "XMLHttpRequest ๊ฐ์ฒด์˜ ํ˜„์žฌ ์ƒํƒœ๋Š” HEADERS_RECEIVED ์ž…๋‹ˆ๋‹ค.<br>";
            break;
        case XMLHttpRequest.LOADING:
            currentState += "XMLHttpRequest ๊ฐ์ฒด์˜ ํ˜„์žฌ ์ƒํƒœ๋Š” LOADING ์ž…๋‹ˆ๋‹ค.<br>";
            break;
        case XMLHttpRequest.DONE:
            currentState += "XMLHttpRequest ๊ฐ์ฒด์˜ ํ˜„์žฌ ์ƒํƒœ๋Š” DONE ์ž…๋‹ˆ๋‹ค.<br>";
            break;
    }
    console.log(currentState);

    if (httpRequest.readyState == XMLHttpRequest.DONE && httpRequest.status == 200) {
        console.log(httpRequest.responseText);
    }
};

httpRequest.open("GET", "/examples/media/ajax_intro_data.txt", true);
httpRequest.send();

 

status ํ”„๋กœํผํ‹ฐ

status ํ”„๋กœํผํ‹ฐ๋Š” ์„œ๋ฒ„์˜ ๋ฌธ์„œ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

  • 200 : ์„œ๋ฒ„์— ๋ฌธ์„œ๊ฐ€ ์กด์žฌํ•จ.
  • 404 : ์„œ๋ฒ„์— ๋ฌธ์„œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Œ.

 

์‘๋‹ต ๋ฐ์ดํ„ฐ ํ”„๋กœํผํ‹ฐ

์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์‘๋‹ต ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋Š” ์•„๋ž˜ ํ”„๋กœํผํ‹ฐ์— ๋“ค์–ด ์žˆ๋‹ค. ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์—์„œ ์ ์ ˆํžˆ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋œ๋‹ค.

  • responseText ํ”„๋กœํผํ‹ฐ : ์„œ๋ฒ„์— ์š”์ฒญํ•˜์—ฌ ์‘๋‹ต์œผ๋กœ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜
  • responseXML ํ”„๋กœํผํ‹ฐ : ์„œ๋ฒ„์— ์š”์ฒญํ•˜์—ฌ ์‘๋‹ต์œผ๋กœ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ XML DOM ๊ฐ์ฒด๋กœ ๋ฐ˜ํ™˜
httpRequest.onreadystatechange = function() {
	// XMLXttpRequest ๊ฐ์ฒด์˜ ํ˜„์žฌ ์ƒํƒœ์™€ ์„œ๋ฒ„ ์ƒ์˜ ๋ฌธ์„œ ์กด์žฌ ์œ ๋ฌด๋ฅผ ํ™•์ธ
    if (httpRequest.readyState == XMLHttpRequest.DONE && httpRequest.status == 200 ) {
    	console.log(httpRequest.responseText); // ์„œ๋ฒ„์— ์š”์ฒญํ•˜์—ฌ ์‘๋‹ต์œผ๋กœ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜
    }
};

# ์ฐธ๊ณ ์ž๋ฃŒ

http://tcpschool.com/ajax/ajax_intro_works