[Ajax] Ajax๋ž€?

hwa.hahaยท2022๋…„ 9์›” 5์ผ
0

Ajax

๋ชฉ๋ก ๋ณด๊ธฐ
1/2
post-thumbnail

๐Ÿ“ŒAjax (Asynchronous JavaScript and XML)

-์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ๋น„๋™๊ธฐ์‹์œผ๋กœ XML์„ ์ด์šฉํ•˜์—ฌ ์„œ๋ฒ„์™€ ํ†ต์‹ ํ•˜๋Š” ๋ฐฉ์‹.
-์ตœ๊ทผ์—๋Š” XML ๋ณด๋‹ค JSON์„ ๋” ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.
-๋น„๋™๊ธฐ์‹์ด๋ž€ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ผ์ด ๋™์‹œ์ ์œผ๋กœ ๋ฐœ์ƒํ•œ๋‹ค๋Š” ๋œป์œผ๋กœ, ์„œ๋ฒ„์™€ ํ†ต์‹ ํ•˜๋Š” ๋™์•ˆ ๋‹ค๋ฅธ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ

EX>์›น์‚ฌ์ดํŠธ์—์„œ ํ‚ค๋ฅผ ํด๋ฆญ ํ•  ๋•Œ๋งˆ๋‹ค ๊ฐ’์ด ๋ณ€๊ฒฝํ•œ๋‹ค,์ง€๋„๊ฐ™์€๋ฐ์„œ ๋“œ๋ž˜๊ทธํ•˜๊ณ  ๋–ผ๋ฉด ๋ณ€๊ฒฝ๋œ ๊ฐ’๋“ค.

๐Ÿ“–Ajax ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ˆœ์„œ

  1. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ด๋ฒคํŠธ
  2. XHR๊ฐ์ฒด ์ƒ์„ฑ ->์„œ๋ฒ„์™€์˜ ํ†ต์‹ ์„ ์œ„ํ•˜์—ฌ
  3. ์„œ๋ฒ„(URL) ์—๊ฒŒ ์š”์ฒญ
  4. ์„œ๋ฒ„์˜ ๊ฒฐ๊ณผ ๊ฐ’ ์–ด๋–ค ํฌ๋ฉง ์ธ์ง€ ํ™•์ธ(text,json,xml)
  5. ์ฝœ๋ฐฑํ•จ์ˆ˜ = > ์ผ์ •ํ•œ HTMLํ™”

โœ์ž…๋ ฅ1-1

//httpRequest.js
function getXMLHttpRequest() {
	if (window.ActiveXObject) {//๊ตฌํ˜•๋ธŒ๋ผ์šฐ์ €์ผ๋•Œ ๊ฐ์ฒด์ƒ์„ฑ๋ฐฉ๋ฒ•.
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e1) { return null; }
		}
	} else if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); //ํ‘œ์ค€๊ฐ์ฒด
	} else {
		return null;
	}
}
var httpRequest = null; //XHR๊ฐ์ฒด

function sendRequest(url, params, callback, method) {
  /*send๋กœ ์ž์ฒด์ ์œผ๋กœ ํ•จ์ˆ˜ํ™”์‹œ์ผœ์„œ ajax๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“ฌ. 
  ์ฝœ๋ฐฑ์€ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ˜์‘์„ ๋ด์•ผํ•ด์„œ ํ•„์š”, 
  ๋ฉ”์†Œ๋“œ๋ฐฉ์‹์ด๋ž‘ ๋ฐ์•„ํ„ฐ ๊ฐ’ ์ „๋‹ฌํ• ๋•Œ */
	httpRequest = getXMLHttpRequest(); //xhr๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์คŒ 
	var httpMethod = method ? method : 'GET';
	if (httpMethod != 'GET' && httpMethod != 'POST') {
      //get์€ ํŒŒ๋ผ๋ฏธํ„ฐ post๋Š” ๋ฐ”๋””์— ๋„ฃ๊ตฌ๊ฐ.
		httpMethod = 'GET'; //get์„ ์‚ฌ์šฉํ•˜๊ฒŸ๋‹ค.
	}
	var httpParams = (params == null || params == '') ? null : params;
	var httpUrl = url;
	if (httpMethod == 'GET' && httpParams != null) {
		httpUrl = httpUrl + "?" + httpParams;
	}
	httpRequest.open(httpMethod, httpUrl, true);
  //true๋ฅผ ์ž…๋ ฅํ•ด์•ผ ๋น„๋™๊ธฐ์‹์ด๋ผ๊ณ  ์…‹ํŒ…
	httpRequest.setRequestHeader(
		'Content-Type', 'application/x-www-form-urlencoded');
  //๊ธฐ๋ณธ ํ—ค๋”๊ฐ’์ž„. ,๋’ค์— ๋‚ด์šฉ์€ ์›๋ž˜ ๋‹ค๋ฅธ๊ฑฐ ๋„ฃ์–ด์•ผํ•ด
	httpRequest.onreadystatechange = callback;
  //์ฝœ๋ฐฑํ•จ์ˆ˜ ๋“ฑ๋ก.์ฝœ๋ฐฑํ•จ์ˆ˜์— ๋ญ๋ฅผ ๋„ฃ๋Š”๋ƒ์— ๋‹ค๋ผ ๋‹ฌ๋ผ์ง.
	httpRequest.send(httpMethod == 'POST' ? httpParams : null);
}

โœ์ž…๋ ฅ1-2

//member_json.jsp
<%@ page language="java" contentType="text/plain; charset=EUC-KR"
    pageEncoding="EUC-KR"%>

    {
    	"code": "success",
    	"data": {
    		"member": {
    			"name": "๋ฐ•์ฐฌํ˜ธ",
    			"id": "jQuery",
    			"sno": "1111-2222"    			
    		}	
    	}    
    }

โœ์ž…๋ ฅ1-3

//road_json.html
 <script type="text/javascript" src ="httpRequest.js"></script>
 <script type="text/javascript">
 
  window.onload = function(){
	  sendRequest("member_json.jsp","",viewInfo,"GET");
   //๋ฐ์ดํ„ฐ๊ฐ’์—†์ด ์š”์ฒญํ•˜๋ฉด get๋ฐฉ์‹
  }
 
  function viewInfo(){
	  if(httpRequest.readyState == 4){
		   if(httpRequest.status == 200){
			 var result = eval("("+httpRequest.responseText+")"); 
           //๋ฌธ์ž์—ด => ๊ฐ์ฒด(๋ณ€ํ™˜) eval =>๋ฌธ์ž์—ด์„ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ• ๋•Œ ์‚ฌ์šฉ!
			 if(result.code == 'success'){
				var m = result.data.member;
				var html ='<span>์ด๋ฆ„:' + m.name+",ID:" +m.id+'</span>';
				
				var list = document.getElementById("list");
				list.innerHTML = html;
			  }
			 }
		   }
  }
  
  
 </script>
</head>
<body>
  <div id ="list"></div>
</body>
</html>

๐Ÿ’ป์ถœ๋ ฅ

์˜ˆ์ œ2-1


Ajax๋Š” ์ œ์ด์ฟผ๋ฆฌ ๋ฐฐ์šฐ๊ธฐ์ „์— ์ดํ•ด๋ฅผ ๋•๊ธฐ์œ„ํ•ด ํ•„์š”ํ•œ ํ๋ฆ„์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›Œ๋ณด์•˜๋‹ค.

profile
๊ฐœ๋ฐœ์ž๋กœ ์ฐจ๊ทผ์ฐจ๊ทผ

0๊ฐœ์˜ ๋Œ“๊ธ€