1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>无标题文档</title> 6 7 <script language="javascript" > 8 9 //IE 10 //var h= new ActiveXObject("Msxml2.XMLHTTP"); 11 12 //var p= new ActiveXObject("Microsoft.XMLHTTP"); 13 14 //非IE 15 //var f = new XMLHTTPRequest(); 16 17 //通用代码 18 var hx; 19 20 if(window.XMLHttpRequest)//非IE,IE7及以上 21 { 22 hx= new XMLHttpRequest(); 23 24 alert("以new XMLHttpRequest()形式创建"); 25 } 26 else if(window.ActiveXObject) //IE 27 { 28 try 29 { 30 hx= new ActiveXObject("Msxml2.XMLHTTP"); 31 32 alert("以Msxml2.XMLHTTP形式创建"); 33 } 34 catch(e) 35 { 36 alert(e); 37 38 try 39 { 40 hx= new ActiveXObject("Microsoft.XMLHTTP"); 41 42 alert("以Microsoft.XMLHTTP形式创建"); 43 } 44 catch(e) 45 { 46 alert(e); 47 } 48 } 49 } 50 51 if(!hx) 52 { 53 alert("创建XMLHttpRequest失败"); 54 } 55 else 56 { 57 alert("创建XMLHttpRequest成功"); 58 } 59 60 //1.设置异步请求的URL等信息 请求方法,URL,请求方式(是否异步) 61 hx.open("POST","ajaxTest",true); 62 63 //2.设置回调函数,事件处理器 64 hx.onreadystatechange = getResult; 65 66 //get 67 //hx.send(null); 68 69 hs.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 70 71 //post 72 hx.send("userid=abc"); 73 74 75 //alert("Server" + hx.getAllResponceHeaders("Server")); 76 77 function getResult() 78 { 79 //1-判断请求状态 80 //alert("readyState=" + hx.readyState); 81 82 if (hx.readyState == 4) 83 { 84 if (hx.status == 200) 85 { 86 alert("回调信息=" + hx.responseText) 87 } 88 else 89 { 90 alert("错误代码=" + hx.status + "" + hx.statusText); 91 } 92 } 93 } 94 95 96 </script> 97 </head> 98 99 <body> 100 </body> 101 </html>