var Ajax = function(url, params, onSuccess, method, async, xhrtype) { this.newHttpRequest = function(xhrtype) { if(typeof(xhrtype) == 'string') return new ActiveXObject(xhrtype); else return typeof(XMLHttpRequest) != 'undefined' ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); } this.xhr = this.newHttpRequest(xhrtype); this.method = typeof(method) == 'string' && method.length > 0 ? method :'POST'; this.async = typeof(async) == 'boolean' ? async : true; this.type = 'TEXT'; this.url = url; this.params = params; this.username = null; this.password = null; this.data = null; this.timeout = -1; this.timeoutID = null; this.completed = false; this.canceled = false; this.onSuccess = typeof(onSuccess) == 'function' ? onSuccess : null; this.onTimeout = function(){ alert('서버 응답 대기 시간을 초과했습니다.'); } this.onError = function(xhr, data) { alert('오류가 발생했습니다. {0} - 상태 값: {1}'.format(this.xhr.status, this.xhr.statusText)); } this.getParams = function() { if(this.params == null) return ''; var sb = new StringBuffer(), cnt = 0; for (var param in this.params) { if(typeof(eval('this.' + param)) == 'function') continue; if (cnt++ > 0) sb.append("&"); sb.append(param).append("=").append(encodeURIComponent(eval('this.params.' + param))); } return sb.toString(); } this.send = function(async, type) { if(typeof(async) != 'undefined') this.async = async; if(typeof(type) != 'undefined') this.type = type; var req = this; var httpParams = this.getParams(); if(this.method == 'GET') this.url = this.url + "?" + httpParams; if(this.username != null && this.password != null) this.xhr.open(this.method, this.url, this.async, this.username, this.password); else this.xhr.open(this.method, this.url, this.async); if(this.async) this.xhr.onreadystatechange = function() { req.onStateChange.call(req); } if(this.timeout > 0) { if(typeof(this.xhr.timeout) != 'undefined') { this.xhr.timeout = this.timeout; //비동기. 동기 상관없이 타임아웃이 잘 동작함 if(this.onTimeout) this.xhr.ontimeout = this.onTimeout; } else { //비동기모드일때만 동작함. this.timeoutID = window.setTimeout( function() { req.onStateChange.call(req, 'timeout'); }, 5000); } } this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); var result = null; try { this.xhr.send(this.method == 'POST' ? httpParams : null); result = !this.async ? (this.type == 'XML' ? this.xhr.responseXML : this.xhr.responseText) : null; } catch (e) { alert('서버와 연결할 수 없습니다.'); } // if(!this.async) { // this.completed = true; // if(this.async && this.timeoutID) // clearTimeout(this.timeoutID); // } return result; } this.setTimeout = function(timeout, onTimeout) { this.timeout = timeout; this.onTimeout = typeof(onTimeout) == 'function' ? onTimeout : this.onTimeout; } this.onStateChange = function(timeout) { var rs = this.xhr.readyState; if(timeout == 'timeout' && this.timeout > 0) { if(rs < 4) { this.xhr.abort(); this.canceled = true; if(this.completed) clearTimeout(this.timeoutID); else this.onTimeout(); } return; } if (this.async && rs == 4) { var status = this.xhr.status; if (status == 200) { if(this.type == 'XML') { if(this.data == null) this.onSuccess(this.xhr.responseXML); else this.onSuccess(this.xhr.responseXML, this.data); } else { if(this.data == null) this.onSuccess(this.xhr.responseText); else this.onSuccess(this.xhr.responseText, this.data); } } else if (status > 0){ this.onError(this.xhr, this.data); } } } this.setType = function(type) { if(type == 'XML' || type == 'TEXT') this.type = type; else alert("{0} 는 사용할 수 없습니다.".format(type)); } this.setAsync = function(flag) { this.async = flag; }; this.setTemp = function(tmp) { this.data = tmp; } }