1 function Ajax(recvType){ 2 var aj=new Object(); 3 aj.recvType=recvType ? recvType.toUpperCase() : ‘HTML‘ //HTML XML 4 aj.targetUrl=‘‘; 5 aj.sendString=‘‘; 6 aj.resultHandle=null; 7 aj.createXMLHttpRequest=function(){ 8 var request=false; 9 //window对象中有XMLHttpRequest存在就是非IE,包括(IE7,IE8) 10 if(window.XMLHttpRequest){ 11 request=new XMLHttpRequest(); 12 if(request.overrideMimeType){ 13 request.overrideMimeType("text/xml"); 14 } 15 //window对象中有ActiveXObject属性存在就是IE 16 }else if(window.ActiveXObject){ 17 var versions=[‘Microsoft.XMLHTTP‘, ‘MSXML.XMLHTTP‘, ‘Msxml2.XMLHTTP.7.0‘,‘Msxml2.XMLHTTP.6.0‘,‘Msxml2.XMLHTTP.5.0‘, ‘Msxml2.XMLHTTP.4.0‘, ‘MSXML2.XMLHTTP.3.0‘, ‘MSXML2.XMLHTTP‘]; 18 for(var i=0; i<versions.length; i++){ 19 try{ 20 request=new ActiveXObject(versions[i]); 21 if(request){ 22 return request; 23 } 24 }catch(e){ 25 request=false; 26 } 27 } 28 } 29 return request; 30 } 31 aj.XMLHttpRequest=aj.createXMLHttpRequest(); 32 aj.processHandle=function(){ 33 if(aj.XMLHttpRequest.readyState == 4){ 34 if(aj.XMLHttpRequest.status == 200){ 35 if(aj.recvType=="HTML") 36 aj.resultHandle(aj.XMLHttpRequest.responseText); 37 else if(aj.recvType=="XML") 38 aj.resultHandle(aj.XMLHttpRequest.responseXML); 39 } 40 } 41 } 42 43 aj.get=function(targetUrl, resultHandle){ 44 aj.targetUrl=targetUrl; 45 46 if(resultHandle!=null){ 47 aj.XMLHttpRequest.onreadystatechange=aj.processHandle; 48 aj.resultHandle=resultHandle; 49 } 50 if(window.XMLHttpRequest){ 51 aj.XMLHttpRequest.open("get", aj.targetUrl); 52 aj.XMLHttpRequest.send(null); 53 }else{ 54 aj.XMLHttpRequest.open("get", aj.targetUrl, true); 55 aj.XMLHttpRequest.send(); 56 } 57 } 58 59 aj.post=function(targetUrl, sendString, resultHandle){ 60 aj.targetUrl=targetUrl; 61 if(typeof(sendString)=="object"){ 62 var str=""; 63 for(var pro in sendString){ 64 str+=pro+"="+sendString[pro]+"&"; 65 } 66 aj.sendString=str.substr(0, str.length-1); 67 }else{ 68 aj.sendString=sendString; 69 } 70 if(resultHandle!=null){ 71 aj.XMLHttpRequest.onreadystatechange=aj.processHandle; 72 aj.resultHandle=resultHandle; 73 } 74 aj.XMLHttpRequest.open("post", targetUrl); 75 aj.XMLHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 76 aj.XMLHttpRequest.send(aj.sendString); 77 } 78 return aj; 79 }
<script type="text/javascript"> var ajax = Ajax(); // 实例化对象,默认为 HTTP,如果传入 XML,返回 XML 对象 // Ajax 的 get 方法使用说明 /** * function(targetUrl, resultHandle) * @param string targetUrl 传递过去的 URL 地址 * @param string resultHandle 回调函数,可选项 */ ajax.get(‘test.php?name=liruxing&[email protected]‘, function(data) { eval("var obj="+data); alert(obj.name); alert(obj.email); }); // Ajax 的 post 方法使用说明 /** * function(targetUrl, sendString, resultHandle) * @param string targetUrl 传递过去的 URL 地址 * @param string sendString 参数值 * @param string resultHandle 回调函数,可选项 */ ajax.post(‘test.php‘, ‘name=liruxing&[email protected]‘, function(data){ eval("var obj="+data); alert(obj.name); alert(obj.email); }) // post 第二种格式,第二个参数为 Json 格式 ajax.post(‘test.php‘, {name:‘李茹星‘,email:‘[email protected]‘}, function(data){ eval("var obj="+data); alert(obj.name); alert(obj.email); }) </script>
时间: 2024-10-08 20:50:29