页面text1
1 <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <title>无标题文档</title> 7 <script type="text/javascript" src="untitled.js" language="javascript"> 8 </script> 9 </head> 10 11 <body> 12 <input id="userName" /> 13 <input id="IsHaveUser" type="button" value="检查用户是否被注册" onclick="startRequest();"/> 14 <span id="validateMessage" ></span> 15 </body> 16 </html>
text2.jsp
1 <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <title>无标题文档</title> 7 <% 8 9 String st=request.getParameter("xx").toString(); 10 if (st.equals("few")) 11 out.print("cuowu"); 12 else 13 out.print("fwe"); 14 %> 15 </head> 16 17 <body> 18 </body> 19 </html>
untitled.js
1 // JavaScript Document 2 var xmlhttp; 3 function createXMLHttpRequest() 4 { 5 //针对IE浏览器 6 if(window.ActiveXObject) 7 { 8 try{ 9 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 10 } 11 catch(e){ 12 try 13 { 14 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 15 } 16 catch(e2){ 17 xmlHttp= false; 18 } 19 } 20 } 21 //其他浏览器 22 else{ 23 xmlhttp=new XMLHttpRequest(); 24 } 25 } 26 function startRequest(url) 27 { 28 var strTe=document.getElementById("userName").value; 29 createXMLHttpRequest(); 30 xmlhttp.onreadystatechange=handleStateChange; 31 xmlhttp.open("POST","text2.jsp?xx="+strTe,true); 32 xmlhttp.send(null); 33 } 34 //回调函数 35 function handleStateChange() 36 { 37 var h=document.getElementById("validateMessage"); 38 if(xmlhttp.readyState==4) 39 { 40 if(xmlhttp.status==200) 41 { 42 h.innerHTML=xmlhttp.responseText; 43 } 44 else if(xmlhttp.status==404) 45 { 46 h.innerHTML="<br>找不到请求的服务器资源!"; 47 } 48 } 49 }
现在就是点击按钮没反应
untitled.js配置路径对么,这个js中的startRequest(url)函数有没有被触发?
谷歌浏览器中正常
IE中无反应
因为IE版本不同,所以创建对象的时候要试着多种版本
if(window.ActiveXObject) 7 { 8 try{ 9 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 10 } 11 catch(e){ 12 try 13 { 14 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 15 } 16 catch(e2){ 17 xmlHttp= false; 18 } 19 } 20 } 21 //其他浏览器 22 else{ 23 xmlhttp=new XMLHttpRequest(); 24 }
把这一段改了就行了