article id #174
categorized under programming & written by 정환
categorized under programming & written by 정환
AJAX를 사용할때는 XMLHttpRequest 객체를 만들어서 사용해야 하는데, 페이지마다 항상 반복적인
코드가 필요하기 떄문에 자바스크립트 파일로 따로 분리해주도록 한다~
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; }
var httpRequest = null;
function sendRequest(url, params, callback, method) {
httpRequest = getXMLHttpRequest();
var httpMethod = method ? method : 'GET';
if(httpMethod != 'GET' && httpMethod != 'POST') {
httpMethod = 'GET';
}
var httpParams = (params == null || params == '') ? null : params;
var httpUrl = url;
if(httpMethod == 'GET' && httpParams != null) {
httpUrl = httpUrl + "?" + httpParams;
}
httpRequest.open(httpMethod, httpUrl, true);
httpRequest.setRequestHeader('Content-Type','appliaction/x-www-form-urlencoded');
httpRequest.onreadystatechange = callback;
httpRequest.send(httpMethod == 'POST' ? httpParams : null);
}
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; }
var httpRequest = null;
function sendRequest(url, params, callback, method) {
httpRequest = getXMLHttpRequest();
var httpMethod = method ? method : 'GET';
if(httpMethod != 'GET' && httpMethod != 'POST') {
httpMethod = 'GET';
}
var httpParams = (params == null || params == '') ? null : params;
var httpUrl = url;
if(httpMethod == 'GET' && httpParams != null) {
httpUrl = httpUrl + "?" + httpParams;
}
httpRequest.open(httpMethod, httpUrl, true);
httpRequest.setRequestHeader('Content-Type','appliaction/x-www-form-urlencoded');
httpRequest.onreadystatechange = callback;
httpRequest.send(httpMethod == 'POST' ? httpParams : null);
}



httpRequest.js


