Ajax原生

来源:互联网 发布:大数据企业 编辑:程序博客网 时间:2024/05/04 22:39

GET:

 

  1.             var sname = document.queryForm.sname.value;  
  2.               
  3.         var xmlHttp;  
  4. try {  
  5.     // Firefox, Opera 8.0+, Safari  
  6.     xmlHttp = new XMLHttpRequest();  
  7. catch (e) {  
  8.     // Internet Explorer  
  9.     try {  
  10.         xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");  
  11.     } catch (e) {  
  12.         try {  
  13.             xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
  14.         } catch (e) {  
  15.             alert("您的浏览器不支持AJAX!");  
  16.             return;  
  17.         }  
  18.     }  
  19. }  
  20.         xmlHttp.open("GET""query.do?sname="+sname, true);               
  21.         xmlHttp.onreadystatechange=function() {  
  22.             if (xmlHttp.readyState==4) {  
  23.                 result.innerHTML = xmlHttp.responseText;  
  24.                 //xmlHttp.responseText还有一种写法:xmlHttp.responseXml  
  25.                 //你可以得到后台传过来的xml,用Javascript来分析,得到需要的数据  
  26.                 //必须掌握xml的分析技术  
  27.             }  
  28.             else{  
  29.                 result.innerHTML = "正在查询,请稍等";  
  30.             }                 
  31.         }  
  32.         xmlHttp.send();  

 POST:

  1. var url = "/xxx.jsp";  
  2. var postStr = "xxx=" + v;  
  3. var eID = "xxx";  
  4. var xmlHttp;  
  5. try {  
  6.     // Firefox, Opera 8.0+, Safari  
  7.     xmlHttp = new XMLHttpRequest();  
  8. catch (e) {  
  9.     // Internet Explorer  
  10.     try {  
  11.         xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");  
  12.     } catch (e) {  
  13.         try {  
  14.             xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
  15.         } catch (e) {  
  16.             alert("您的浏览器不支持AJAX!");  
  17.             return;  
  18.         }  
  19.     }  
  20. }  
  21. xmlHttp.open("POST", url, true);  
  22. xmlHttp.setRequestHeader("Content-Type",  
  23.         "application/x-www-form-urlencoded");  
  24. xmlHttp.onreadystatechange = function() {  
  25.     if (xmlHttp.readyState == 4) {  
  26.         if (xmlHttp.responseText == 1) {  
  27.             gB(eID).innerHTML = nr + "<font color=blue> 正确</font>" 
  28.         } else {  
  29.             gB(eID).innerHTML = img + '<b><font color=red>' 
  30.                     + xmlHttp.responseText + '</font></b>';  
  31.         }  
  32.     } else {  
  33.         gB(eID).innerHTML = "检查中";  
  34.     } 
0 0
原创粉丝点击