方法不错!
var xmlhttp = false,isComplete = false;//创建XMLHttpRequest对象
function createXMLHttpRequest() {
if(window.XMLHttpRequest) { //Mozilla 浏览器
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject) { // IE浏览器
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
}
function Ajax(){
createXMLHttpRequest();
if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){
xmlhttp = new XMLHttpRequest()
}
this.open = function(url, method, feedback,process, arg,asyc){
if (!xmlhttp) return false;
isComplete = false;
var asy;
if(asyc==null){asy=true;}else{asy=false;}
if(arguments.length==2){feedback=method;method="post";}
method = method.toUpperCase();
try{
xmlhttp.setRequestHeader("Cache-Control: no-store, no-cache, must-revalidate");
xmlhttp.setRequestHeader("Connection","close");
}catch(e){}
try{
if (method == "GET"){
xmlhttp.open(method, url+"?"+arg,asy);
arg = "";
}else{
xmlhttp.open(method, url, asy);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}
xmlhttp.send(arg);
getValue_timer=setTimeout("this.Abort()",60000);
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && !isComplete){
isComplete = true;
clearTimeout(getValue_timer);
if(xmlhttp.status==200||xmlhttp.status==0){
try{var json=eval(xmlhttp.responseText);}catch(e){var json="not support";}
try{feedback(xmlhttp.responseText,xmlhttp.responseXML,json);}catch(e){}
}else{
try{feedback(xmlhttp.status);}catch(e){}
}
}else{
try{process(xmlhttp.readyState);}catch(e){}
}
};
}
catch(z) { return false; }
return true;
};
this.Abort=function(){alert('获取数据失败');try{this.abort();}catch(e){}};
return this;
};
//post ("/login.do",signOnOff,"action=Bsign&object=Member")
function post(url,done,arg){
var ajax=new Ajax();
var load=function(){};
ajax.open(url,"POST",done,load,arg);
}
function get(url,done,arg){
var ajax=new Ajax();
var load=function(){};
ajax.open(url,"GET",done,load,arg);
}
//form表单中的数连接
function convertFormObjs2Arrays(formObj){
for(var x = 0; x < formObj.length; x++){
var item =formObj[x];
paramArray+=item.name+ '=' +item.value+ '&';
}
return paramArray;
}
阅读(1215) | 评论(0) | 转发(0) |