2006/02/25

동적 웹페이지를 위한 기본 AJAX 클래스 코드

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;
//        }



출처 :
http://www.oraclejava.co.kr/zboard/view.php?id=php&page=1&sn1=&divpage=1&sn=off&ss=on&sc=on&select_arrange=headnum
&desc=asc&no=39


댓글 없음:

댓글 쓰기

가장 많이 본 글