Javascript无法在Firefox中运行,但在IE中可以正常工作.
问题描述:
function GetData()
{
var objXML,bNewData,strUserID,strCoID,strAdmin;
var strUserName,bNewData;
var strSQL,objNode,xmlDoc;
var xmlhttp;
var objNode,objNodes;
strUserID = Form1.txtUserID.value;
strCoID= Form1.txtCoID.value;
strAdmin= Form1.chkIsAdmin.checked;
doClearTable(document.all.tblUsers);
strSQL = 'url';
bNewData = false;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",strSQL,false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
objNodes=xmlhttp.evaluate(xmlDoc,xmlhttp,null,null,null);
for(objNode=0; objNode<objNodes.length; objNode++)
{
strUserName=objNodes[objNode].selectSingleNode("UserName").text;
strUserID=objNodes[objNode].selectSingleNode("ID").text;
doAddSingleUserRow(strUserName,tblUsers,strUserID);
}
}
//
上面的Javascript方法在IE中工作正常,但在Firefox中不工作.
如果有人能解决?在Firefox中,ResponseXML给出了undefined.
Above Javascript method is working fine in IE but not working in Firefox.
If anyone can solve? In Firefox, ResponseXML gives undefined.
答
好,您对XMLHttprequest的实现还不完整.在尝试访问/使用responseXML之前,您需要检查各种状态.
请查看以下链接,以了解有关实现的更多信息以及应该在其中进行的各种检查:
XMLHttpRequest对象 [带有示例的XMLHTTPRequest [使用XML HTTP请求对象 [
Well, your implementation of XMLHttprequest is not entirely complete. You need to check for various status before trying to access/use the responseXML.
Have a look at these links to know more about the implementation and various checks that should be there:
XMLHttpRequest Object[^]
XMLHTTPRequest with Sample[^]
Using the XML HTTP Request object[^]
用"xmlHttp.responseText"替换"xmlhttp.responseXML"
Replace "xmlhttp.responseXML" by "xmlHttp.responseText"