javascript 无法读取 null 的属性“getElementsByTagName”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27331470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot read property 'getElementsByTagName' of null
提问by Charles Kiel
I've been searching around for answers to this particular problem for about two days now. I can't seem to figure out what's going on with it. All this seemed to have worked in the past but isn't working anymore.
我一直在寻找这个特定问题的答案大约两天了。我似乎无法弄清楚这是怎么回事。所有这些似乎在过去都有效,但不再有效。
XML:
XML:
<ZipCodes>
<results>
<city>Chicago</city>
<state>IL</state>
<timezone>-6</timezone>
</results>
javascript:
javascript:
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","_checkzip.php?zip="+str,true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
x=xmlDoc.getElementsByTagName("results");
i=0;
document.forms["signup"]["City"].value=(x[i].getElementsByTagName("city")[0].childNodes[0].nodeValue);
document.forms["signup"]["states"].value=(x[i].getElementsByTagName("state")[0].childNodes[0].nodeValue);
document.forms["signup"]["TimeZone"].value=(x[i].getElementsByTagName("timezone")[0].childNodes[0].nodeValue);
and the error I get is
我得到的错误是
Cannot read property 'getElementsByTagName' of null
when I try x=xmlDoc.getElementsByTagName("results"); The xml is deffinetly coming in on the network response when I look at the debug in Chrome.
当我尝试 x=xmlDoc.getElementsByTagName("results"); 当我在 Chrome 中查看调试时,xml 肯定会进入网络响应。
采纳答案by Aeveus
You can use an event handler on an XMLHttpRequest's onreadystatechange
to make sure the request is done. If the status of readyState is 4, the operation is complete.
您可以在一个使用事件处理程序的XMLHttpRequest的onreadystatechange
,以确保该请求被完成。如果 readyState 的状态为 4,则操作完成。
Currently you're asking for data that hasn't been transmitted yet.
目前您正在请求尚未传输的数据。
回答by AfamO
Apart from ensuring that xmlhttp.readyState==4 && xmlhttp.status==200, this error can also occur if you outputed any content, letter, character or string before sending your xml data from the server. For instance Assuming that in java you have a method/function called sendXMLdata, and then in your servlet you printed the string "connected" before calling the method: .... out.println("Connected"); String data="My Name" sendXMLdata(response,data); ....
除了确保 xmlhttp.readyState==4 && xmlhttp.status==200 之外,如果您在从服务器发送 xml 数据之前输出了任何内容、字母、字符或字符串,也会发生此错误。例如,假设在 Java 中有一个名为 sendXMLdata 的方法/函数,然后在您的 servlet 中,您在调用该方法之前打印了字符串“connected”: .... out.println("Connected"); String data="My Name" sendXMLdata(response,data); ....
Then the output when viewed from browser source will be this: Connected My Name
然后从浏览器源查看时的输出将是这样的:Connected My Name
So this will certainly give you an error since the browser would not be able to parse the string "connected" since it is not the write format to render xml data.
所以这肯定会给你一个错误,因为浏览器将无法解析字符串“connected”,因为它不是呈现 xml 数据的写入格式。