[Web] Ajax 간단하게!! 이해하기!!!

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">

var xhr = null;
function getXMLHttpRequest() {
if (window.ActiveXObject) {  // IE 8 이상
try {
return new ActiveXObject("Msxml2.XMLHTTP");
} catch (e1) {
try {
return new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
return null;
}
}
} else if (window.XMLHttpRequest) {   // 모질라, 사파리 등 그외 브라우저
return new XMLHttpRequest();
} else {
return null;
}
}

function requestHello(URL) {
param = f.name.value;
URL = URL + "?name=" + encodeURIComponent(param);
xhr = getXMLHttpRequest();
xhr.onreadystatechange = responseHello;
xhr.open("GET", URL, true);
xhr.send(null);
}

function requestHello2(URL) {
URL = URL + "?list";
xhr = getXMLHttpRequest();
xhr.onreadystatechange = responseHello;
xhr.open("GET", URL, true);
xhr.send(null);
}

function responseHello(){
if(xhr.readyState == 4){  //이상없음, 응답 받았음
var str = xhr.responseText;
document.getElementById("message").innerHTML = str;
}else{   //아직 준비 되지 않음
alert(!"Fail : "+httpRequest.status);
}
}
</script>
</head>
<body>
<form name="f">
<input type="text" name="name">
<input type="button" value="입력 " onclick="requestHello('uinfo.jsp')">
<input type="button" value="리스트 " onclick="requestHello2('ulist.jsp')">
</form>
<div id="message"></div>

</body>
</html>


-- 이건 일단 main.jsp  그냥 jsp 아니어도 html 도 상관없음






댓글