본문 바로가기

Java/AJAX

Simple한 Ajax 예제

=======================================================

simpleAjaxApp.html

=======================================================

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">

<head>

<meta http-equiv="content-type" content="text/html; charset=euc-kr" />

<title>간단한 Ajax 어플리케이션!</title>

<script type="text/javascript">

var httpRequest = null;

 

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;

}

}

function load(url) {

httpRequest = getXMLHttpRequest();

//open() 함수와 send()함수를 사용하여 서버에 데이터를 전송한 다음에 작업은

//서버로부터 응답이 도착하면 알맞게 처리하는 것이다.

httpRequest.onreadystatechange = viewMessage;

//open() 함수 : 요청의 초기화. GET방식으로 url 곳으로 비동기식으로 접속한다는 .

//httpRequest.open("GET", "/test.jsp?id=madvirus&pw=1234", true);

httpRequest.open("GET", url, true);

//send() 함수 : 웹서버에 요청 전송. POST방식일 경우에만

//httpRequest.send("id=madvirus&pw=1234"); 이런 식으로 보낸다.

httpRequest.send(null);

}

function viewMessage() {

if (httpRequest.readyState == 4) {

if (httpRequest.status == 200) {

alert(httpRequest.responseText);

} else {

alert("실패: "+httpRequest.status);

}

}

}

</script>

</head>

<body>

<input type="button" value="simple.txt" onclick="load('simple.txt')"/>

<input type="button" value="simple2.txt" onclick="load('simple2.txt')"/>

<input type="button" value="simple.jsp" onclick="load('simple.jsp')"/>

<input type="button" value="simple2.jsp" onclick="load('simple2.jsp')"/>

</body>

</html>


출처 : 최범균의 AJAX

'Java > AJAX' 카테고리의 다른 글

clock.html  (0) 2009.08.04
XMLHttpRequest 모듈 : httpRequest.js  (0) 2009.08.03