// 提示消息
function messageDiv(title) { var messageDiv = document.createElement('' ); messageDiv .innerHTML = '+ screen.height + '" id="message">' + '' + '' + ' ' + title + ', 连接未初始化... | '; var length = document.getElementsByName ("message").length; messageDiv .style.cssText = "position:absolute;bottom:" + (length * 24 ) + "px;left:0px;display:none"; document.body.appendChild(messageDiv ); this.clear = function () { document.body.removeChild(messageDiv ); var msg = document.getElementsByName ("message"); for (var i =0 ;i <msg .length;i ++){ msg [i ].parentNode.style.cssText = "position:absolute;bottom:" + (i *24 ) + "px;left:0px"; } } this.showMsg = function (message ) { messageDiv .style.display = ""; messageDiv .all.Span1 .innerHTML += ' ' + message ; }}// 兼容IE与其它浏览器(From Sajax.php v0.12)
function createXMLHttpRequest() { var requestObject; var _msxmlhttp = new Array( 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'); for(var i = 0; i < _msxmlhttp.length; i++) { try { if(requestObject = new ActiveXObject(_msxmlhttp[i])) { break; } } catch (e) { requestObject = null; } } if(!requestObject && typeof XMLHttpRequest != 'undefined') { requestObject = new XMLHttpRequest(); }
if(!requestObject) { alert('Could not create connection object.'); }
return requestObject; }
var submitMethodConst = new Object(); submitMethodConst.POST = 'POST'; submitMethodConst.GET = 'GET';
function AjaxBase() {
this.author = 'QBQ'; this.version = '2009-12-10 build1.0a';
var requestObject = new createXMLHttpRequest();
this.defaultRequest = function(url, callback, postString) { this.request(url, callback, true, submitMethodConst.POST, postString, false); }
/** * * @param {} url 请求页面URL(必填) * @param {} callback 返回值处理函数(必填) * @param {} isAsync 是否异步 ,(ture|false), 默认true * @param {} 请求方式,(submitMethodConst.POST | submitMethodConst.GET), 默认get * @param {} postString 请求方式为post时,请求内容 * @param {} messageTitle 请求内容标题 */ this.request = function (url, callback, isAsync, submitMethod, postString, messageTitle, cache) {
var async = true; var method = submitMethodConst.GET; var params = ''; var title = '请等待'; var useCache = false;
async = isAsync; useCache = cache;
if (submitMethod) { method = submitMethod; }
if (typeof(postString) == 'string') { params = postString; if (!useCache) { params += '&sid=' + Math.random(); } }
if (messageTitle != '' && typeof(messageTitle) == 'string') { title = "正在获取" + messageTitle; }
var msgbox = new messageDiv(title);
requestObject.onreadystatechange = function () { if (async) { switch (requestObject.readystate) { case 1: msgbox.showMsg("正在初始化连接..."); return; case 2: msgbox.showMsg("正在发送数据..."); return; case 3: msgbox.showMsg("正在接收数据..."); return; case 4: msgbox.showMsg("数据接收完成..."); if (requestObject.status == 200) { callback(requestObject.responseText); setTimeout(msgbox.clear, 1000); } else { msgbox.showMsg("请求失败," + requestObject.statustext + "(" + requestObject.status + ")"); setTimeout(msgbox.clear, 3000); } return; } } }
requestObject.open (method, url, async);
if (method == 'POST') { msgbox.showMsg("正在接收数据..."); requestObject.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); // 这句一定不能缺,否则参数传不到后台
requestObject.send(params); } else { requestObject.send(null); }
if (!async) { msgbox.showMsg("数据接收完成..."); callback(requestObject.responseText); msgbox.clear(); } }; }
|