“空” IE javascript 中为 null 或不是对象错误
出现错误:
'null' is null or not an object
以下代码在 Firefox 和 Chrome 中执行正常,但在 Internet Explorer 中执行时
if (xmlhttp.responseXML != null)
{
var xmlDoc = xmlhttp.responseXML.documentElement ;
var ResultNodes = xmlDoc.getElementsByTagName ("Result") ; <---- error here
if (ResultNodes != null)
{
(我本以为指示的那一行后面的行更有可能返回错误,但调试器说运行时错误位于指示的行处)
有什么想法吗?
The following code executes fine in Firefox and Chrome, but gives an error:
'null' is null or not an object
when executed in Internet Explorer.
if (xmlhttp.responseXML != null)
{
var xmlDoc = xmlhttp.responseXML.documentElement ;
var ResultNodes = xmlDoc.getElementsByTagName ("Result") ; <---- error here
if (ResultNodes != null)
{
(I would have thought the line after the one indicated would be more likely to return the error but the debugger says the run-time error is at the line indicated)
Any ideas why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这样的事情(像往常一样,IE 的做法有所不同)(取自 http://msdn.microsoft.com/en-us/library/ms534370(VS.85).aspx)
Try something like this (as usual, IE does things diferently) (take from http://msdn.microsoft.com/en-us/library/ms534370(VS.85).aspx )
我想我只是报告我的发现,现在我已经一切正常了。以下客户端代码(略有删节和匿名)包含解决本线程中概述的问题所需的所有解决方法,并且适用于 IE (8.0.6001)、FF(3.5.9) 和 Chrome (5.0.6)。 375.55 测试版)。尚未在旧版本浏览器下进行测试。非常感谢所有回复的人。
我还应该补充一点,我需要确保服务器响应需要包括:
才能与 IE 一起使用。 FF 不介意 ContentType 是否为 text/HTML,但 IE 却咳嗽了。
创建 XMLHTTP 请求的代码:
返回记录节点文本值的代码:
执行 AJAX 请求的代码:
Thought I would just report back my findings, now that I have it all working. The following client-side code (slightly abridged and anonymized) contains all the work-arounds I needed to address the prblems outlined in this thread and works on IE (8.0.6001), FF(3.5.9), and Chrome (5.0.375.55 beta). Still yet to test under older versions of browsers. Many thanks to all who responded.
I should also add that I needed to make sure that the server response needed to include:
for it to work with IE. FF didn't mind if the ContentType was text/HTML but IE coughed.
Code to create an XMLHTTP request:
Code to return the text value of a record node:
Code to perform the AJAX request:
我刚刚找到了解决这个问题的方法。在xml文件中将编码类型设置为us-ascii(encoding='us-ascii')。它解决了我的问题。
I've just found a solution for this. Make the encoding type us-ascii (encoding='us-ascii') in the xml file. It solved my problem.