XMLHttpRequest javascript 용 class v1.0.0
ITWeb/개발일반 2010. 5. 11. 12:26여기 저기 널린 코드들은 많은데 맘에 드는 코드가 별로 없어서 기냥 만들었습니다.
도대체 코드를 보기 힘들게 작성하는 이유는 무슨 심리 인지.. 좀 쉽게 작성 하면 어디 덧나나.. ㅡ.ㅡ;;
참고 사이트 : http://www.w3.org/TR/XMLHttpRequest/
뭐 아래 코드가 잘 짜진것도 아니지만.. 보기 쉽게 하려고 쬐금 노력은 했다는.. ㅎㅎ
이것도 보기 힘들다면.. 패스~
/*
* XMLHttpRequest Control Module
*
* @package
* @path
* @filename xhr.js
* @author
* @date 2010/05/03
*
* Change History
* Date Engineer Type Description
* ---------- ------------------ --------- --------------------
* 2010/05/03 Henry Jeong create initialize
*/
도대체 코드를 보기 힘들게 작성하는 이유는 무슨 심리 인지.. 좀 쉽게 작성 하면 어디 덧나나.. ㅡ.ㅡ;;
참고 사이트 : http://www.w3.org/TR/XMLHttpRequest/
뭐 아래 코드가 잘 짜진것도 아니지만.. 보기 쉽게 하려고 쬐금 노력은 했다는.. ㅎㅎ
이것도 보기 힘들다면.. 패스~
/*
* XMLHttpRequest Control Module
*
* @package
* @path
* @filename xhr.js
* @author
* @date 2010/05/03
*
* Change History
* Date Engineer Type Description
* ---------- ------------------ --------- --------------------
* 2010/05/03 Henry Jeong create initialize
*/
/*
*
* XMLHttpRequest module object
*
*/
var JXhr = function() {
};
*
* XMLHttpRequest module object
*
*/
var JXhr = function() {
};
/*
*
* xmlhttprequest variables
*/
JXhr.prototype.vars = {
xhr : null,
onreadystatechange : null,
onload : null,
onerror : null,
onresponse : null,
contenttype : "text/xml",
overridemimetype : "text/xml",
method : "GET",
callUrl : "",
async : true,
header : [], // setRequestHeader
responsedata : "XML", // responseText, responseXML
params : ""
}
*
* xmlhttprequest variables
*/
JXhr.prototype.vars = {
xhr : null,
onreadystatechange : null,
onload : null,
onerror : null,
onresponse : null,
contenttype : "text/xml",
overridemimetype : "text/xml",
method : "GET",
callUrl : "",
async : true,
header : [], // setRequestHeader
responsedata : "XML", // responseText, responseXML
params : ""
}
JXhr.prototype.init = function() {
if (window.XMLHttpRequest) {
JXhr.vars.xhr = new XMLHttpRequest();
JXhr.vars.xhr.onreadystatechange = JXhr.vars.onreadystatechange;
} else if (window.ActiveXObject) {
try {
JXhr.vars.xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
JXhr.vars.xhr = false;
}
if (window.XMLHttpRequest) {
JXhr.vars.xhr = new XMLHttpRequest();
JXhr.vars.xhr.onreadystatechange = JXhr.vars.onreadystatechange;
} else if (window.ActiveXObject) {
try {
JXhr.vars.xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
JXhr.vars.xhr = false;
}
if (JXhr.vars.xhr) {
JXhr.vars.xhr.onreadystatechange = JXhr.vars.onreadystatechange;
}
}
}
JXhr.vars.xhr.onreadystatechange = JXhr.vars.onreadystatechange;
}
}
}
JXhr.prototype.open = function() {
var openUrl = JXhr.vars.callUrl;
var openUrl = JXhr.vars.callUrl;
if (JXhr.vars.method == "GET") {
if (JXhr.vars.params != "") {
openUrl += "?" + JXhr.vars.params;
}
}
if (JXhr.vars.params != "") {
openUrl += "?" + JXhr.vars.params;
}
}
JXhr.vars.xhr.open(JXhr.vars.method, openUrl, JXhr.vars.async);
}
}
JXhr.prototype.send = function() {
switch (JXhr.vars.method) {
case "GET":
JXhr.vars.xhr.send(null);
break;
case "POST":
JXhr.vars.xhr.setRequestHeader("Content-type",
"application/x-www-form-URLencoded");
JXhr.vars.xhr.setRequestHeader("Content-length",
JXhr.vars.params.length);
JXhr.vars.xhr.setRequestHeader("Connection", "close");
JXhr.vars.xhr.send(JXhr.vars.params);
break;
}
}
switch (JXhr.vars.method) {
case "GET":
JXhr.vars.xhr.send(null);
break;
case "POST":
JXhr.vars.xhr.setRequestHeader("Content-type",
"application/x-www-form-URLencoded");
JXhr.vars.xhr.setRequestHeader("Content-length",
JXhr.vars.params.length);
JXhr.vars.xhr.setRequestHeader("Connection", "close");
JXhr.vars.xhr.send(JXhr.vars.params);
break;
}
}
JXhr = new JXhr();
/*
* <script type="text/javascript"> <!-- function getReadyStateChage () { if (
* JXhr.vars.xhr.readyState == 4 ) { if ( JXhr.vars.xhr.status == 200 ||
* JXhr.vars.xhr.statusText == "OK" ) { JXhr.vars.onresponse(); } else {
* document.getElementById("divDebug").innerHTML = JXhr.vars.xhr.statusText + "<br>" +
* document.getElementById("divDebug").innerHTML; } } }
*
* function getResponseData () { if ( JXhr.vars.responsedata == "XML" ) {
* JXhr.vars.xhr.responseXML; } else { JXhr.vars.xhr.responseText; } }
*
* function run() { JXhr.vars.onreadystatechange = getReadyStateChage;
* JXhr.vars.onresponse = getResponseData; JXhr.vars.method = "GET";
* JXhr.vars.callUrl = "http://HOSTNAME/test.txt";
* JXhr.vars.async = true; JXhr.vars.responsedata = "TEXT"; JXhr.vars.params =
* "";
*
* JXhr.init(); JXhr.open(); JXhr.send(); } //--> </script>
*/
* <script type="text/javascript"> <!-- function getReadyStateChage () { if (
* JXhr.vars.xhr.readyState == 4 ) { if ( JXhr.vars.xhr.status == 200 ||
* JXhr.vars.xhr.statusText == "OK" ) { JXhr.vars.onresponse(); } else {
* document.getElementById("divDebug").innerHTML = JXhr.vars.xhr.statusText + "<br>" +
* document.getElementById("divDebug").innerHTML; } } }
*
* function getResponseData () { if ( JXhr.vars.responsedata == "XML" ) {
* JXhr.vars.xhr.responseXML; } else { JXhr.vars.xhr.responseText; } }
*
* function run() { JXhr.vars.onreadystatechange = getReadyStateChage;
* JXhr.vars.onresponse = getResponseData; JXhr.vars.method = "GET";
* JXhr.vars.callUrl = "http://HOSTNAME/test.txt";
* JXhr.vars.async = true; JXhr.vars.responsedata = "TEXT"; JXhr.vars.params =
* "";
*
* JXhr.init(); JXhr.open(); JXhr.send(); } //--> </script>
*/