function Ajax_new(
recvType,url,str,method,recvFunc){//recvType:
格式
url
:地址
method
:方式
recvFunc
:处理结果函式
var aj = new Object();
aj.result = '';
aj.targetUrl = url+'?'+str;
aj.sendString = '';
aj.recvType = recvType ? recvType : 'HTML';//HTML XML
aj.resultHandle = null;
/*创建ajax组件*/
aj.createXMLHttpRequest = function() {//create Ajax 组件
var request = false;
if(window.XMLHttpRequest) {
request = new XMLHttpRequest();
if(request.overrideMimeType) {//设置mime 类别
request.overrideMimeType('text/xml');
}
} else if(window.ActiveXObject) {
var versions = ['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
for(var i=0; i<versions.length; i++) {
try {
request = new ActiveXObject(versions[i]);
if(request) {
return request;
}
} catch(e) {
//alert(e.message);
}
}
}
return request;
}
aj.XMLHttpRequest = aj.createXMLHttpRequest();
/*数据处理*/
if (method == 'get'){
aj.XMLHttpRequest.onreadystatechange = function (){aj_test(aj,recvFunc);};
if(window.XMLHttpRequest) {
aj.XMLHttpRequest.open('GET', aj.targetUrl, false);
aj.XMLHttpRequest.send(null);
} else {
aj.XMLHttpRequest.open("GET", targetUrl, false);
aj.XMLHttpRequest.send();
}
}
else{
}
return aj;
}
function aj_test(obj,recvFunc){//调用相应的结果处理
if(obj.XMLHttpRequest.readyState == 4) {
if(obj.XMLHttpRequest.status == 200) {
if(obj.recvType == 'HTML') {
recvFunc(obj.XMLHttpRequest.responseText);
} else if(obj.recvType == 'XML') {
recvFunc(obj.XMLHttpRequest.responseXML);
}
}
}
}
页面调用: Ajax_new('XML',url,str,'get',ajax_result1);
Function ajax_result1(obj){
。。。。。。。。。。。。。。处理
}