问题描述
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><!-- DW6 --><head><!-- Copyright 2005 Macromedia, Inc. All rights reserved. --><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>Ajax Demo</title><link rel="stylesheet" href="emx_nav_right.css" type="text/css" /><script type="text/javascript"> function login() { var xmlHttp = false; try{ xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari }catch (e){ //Internet Explorer try{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }catch (e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch (e){ alert("111"); return false; } } }var name = document.getElementById("myname"); var myname = name.getAttribute("value"); var password = document.getElementById("mypassword"); var mypassword = password.getAttribute("value"); //var serverUrl ="AjaxServlet"; var requestMethod = "GET"; //httpRequest= new ActiveXObject("Msxml2.XMLHTTP"); xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { document.getElementById("info").innerHTML=xmlHttp.responseText; } } xmlHttp.open(requestMethod,"ajax/Ajax.jsp?name="+myname+"&password="+mypassword,true); xmlHttp.send(null);}</script></head><body> <br><label id="info"> UserName: <input type="text" name="name" id="myname"></input> PassWord: <input type="password" name="password" id="mypassword"></input> <input type="button" name="loginsub" value="Login" onclick="login();"></input>未登陆</label></body></html>整张代码 在IE上面可以获取接收到的 username和password 但是在FireFox里面接收就为null 为什么 问题补充:wazhouqian 写道
解决方案
把那个参数name和password改成var name=document.getElementById("myname").value,密码同上。试试
解决方案二:
var xmlHttp;function createXMLHttpRequest(){ if(window.XMLHttpRequest){ //Mozilla 浏览器 xmlHttp = new XMLHttpRequest(); }else if(window.ActiveXObject) { //IE浏览器 try{ xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){} } } if(xmlHttp == null){ alert("不能创建XMLHttpRequest对象"); return false; }}
解决方案三:
引用 function login() { var xmlHttp = false; try{ xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari }catch (e){ //Internet Explorer try{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }catch (e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch (e){ alert("111"); return false; } } } 为什么给xmlHttp赋值了3次?最好判断什么样的浏览器赋什么样的值