샘플 예제
페이지 정보
작성자 서방님 댓글 1건 조회 397회 작성일 08-08-20 13:37본문
<SCRIPT LANGUAGE="JavaScript">
<!--
var AJAX = {
XmlHttp: null,
create: function () {
try {
if (window.XMLHttpRequest) {
AJAX.XmlHttp = new XMLHttpRequest();
// 일부의 모질라 버전을은 readyState property,
// onreadystate event를 지원하지 않으므로. - from xmlextrs
if (this.XmlHttp.readyState == null) {
this.XmlHttp.readyState = 1;
this.XmlHttp.addEventListener("load", function () {
this.XmlHttp.readyState = 4;
if (typeof this.XmlHttp.onreadystatechange == "function")
tmpXmlHtp.onreadystatechange();
}, false);
}
} else {
AJAX.XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
catch (e) {
alert("Your browser does not support XmlHttp objects");
//throw new Error("Your browser does not support XmlHttp objects");
}
}
}
AJAX.openXML = function (method, url, async, uname, pswd) {
if (AJAX.XmlHttp != null) {
if (uname == undefined) {
AJAX.XmlHttp.open(method, url, async, uname, pswd);
} else {
AJAX.XmlHttp.open(method, url, async);
}
AJAX.XmlHttp.onreadystatechange = function () {
if (AJAX.XmlHttp.readyState == 4) {
if (AJAX.XmlHttp.status == 200) {
// 200 은 HTTP에서 성공 i.e) 404 : not found
AJAX.statusSuccessHandler(AJAX.XmlHttp.responseXML.xml);
} else {
AJAX.statusErrorHandler();
alert('Error while loading!');
}
}
}
} else {
alert ("need to create xmlhttp object");
}
}
AJAX.openText = function (method, url, async, uname, pswd) {
if (AJAX.XmlHttp != null) {
if (uname == undefined) {
AJAX.XmlHttp.open(method, url, async, uname, pswd);
} else {
AJAX.XmlHttp.open(method, url, async);
}
AJAX.XmlHttp.onreadystatechange = function () {
if (AJAX.XmlHttp.readyState == 4) {
if (AJAX.XmlHttp.status == 200) {
// 200 은 HTTP에서 성공 i.e) 404 : not found
AJAX.statusSuccessHandler(AJAX.XmlHttp.responseText);
} else {
AJAX.statusErrorHandler();
alert('Error while loading!');
}
}
}
} else {
alert ("need to create xmlhttp object");
}
}
AJAX.send = function (content) {
if (content == undefined)
AJAX.XmlHttp.send(null);
else
AJAX.XmlHttp.send(content);
}
AJAX.setOnReadyStateChange = function (funcname) {
if (AJAX.XmlHttp) {
AJAX.XmlHttp.onreadystatechange = funcname;
} else {
alert ("need to create xmlhttp object");
}
}
// status 200 일 때 처리 함수
AJAX.statusSuccessHandler = function (data) {
alert(data);
}
AJAX.setStatusSuccessHandler = function (funcname) {
AJAX.statusSuccessHandler = funcname;
}
// status 200 일 때 기본 처리 함수
// setStatusSuccessHandler() 로 대체 가능
AJAX.statusErrorHandler = function (status) {
AJAX.rtnText = AJAX.XmlHttp.responseText;
}
AJAX.setStatusSuccessHandler = function (funcname) {
AJAX.statusSuccessHandler = funcname
}
AJAX.setStatusErrorHandler = function (funcname) {
AJAX.statusErrorHandler = funcname
}
AJAX.setRequestHeader = function (label, value)
{
AJAX.XmlHttp.setRequestHeader(label, value);
}
// 사용법
// AJAX.create();
// AJAX.openText('GET','jsontest.php', true);
// AJAX.setStatusSuccessHandler(proc);
// AJAX.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
// AJAX.send("test=테스트");
//
// function proc(abc)
// {
// document.getElementById('test').innerHTML = abc;
// }
//-->
</SCRIPT>
댓글목록
등록된 댓글이 없습니다.