原生js提交ajax请求
1 <script type="text/javascript"> 2 window.onload = function(){ 3 var btn = document.getElementById("btn"); 4 var pcontent = document.getElementById("content"); 5 //获取XMLHttpRequest 兼容各种浏览器 包括IE6,IE5 6 function getXmlHttp(){ 7 if(window.XMLHttpRequest){ 8 return new XMLHttpRequest(); 9 }else if( window.ActiveXObject){ 10 var version = [ 11 "MSXML2.XMLHttp.6.0", 12 "MSXML2.XMLHttp.3.0", 13 "MSXML2.XMLHttp" 14 ]; 15 for(var i=0;i<version.length;i++){ 16 try { return new ActiveXObject(version[i]);}catch (e) {} 17 } 18 }else{ 19 alert("您的浏览器不支持XMLHttp"); 20 } 21 } 22 btn.onclick = function(){ 23 var xmlHttp = getXmlHttp(); 24 xmlHttp.onreadystatechange = function(){ 25 if(xmlHttp.readyState == 4 ){ 26 if(xmlHttp.status == 200){ 27 pcontent.innerHTML = xmlHttp.responseText; 28 }else{ 29 alert("错误信息:"+ xmlHttp.statusText); 30 } 31 } 32 } 33 xmlHttp.open("GET","data.txt",true); 34 xmlHttp.send(null); 35 } 36 37 }