“空” IE javascript 中为 null 或不是对象错误

发布于 2024-09-03 04:15:56 字数 445 浏览 7 评论 0原文

出现错误:

'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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城泪 2024-09-10 04:15:56

尝试这样的事情(像往常一样,IE 的做法有所不同)(取自 http://msdn.microsoft.com/en-us/library/ms534370(VS.85).aspx)

if (xmlhttp.responseXML.xml)
    var xmlDoc = xmlhttp.responseXML.xml;
else
    var xmlDoc = xmlhttp.responseXML;

Try something like this (as usual, IE does things diferently) (take from http://msdn.microsoft.com/en-us/library/ms534370(VS.85).aspx )

if (xmlhttp.responseXML.xml)
    var xmlDoc = xmlhttp.responseXML.xml;
else
    var xmlDoc = xmlhttp.responseXML;
北城半夏 2024-09-10 04:15:56

我想我只是报告我的发现,现在我已经一切正常了。以下客户端代码(略有删节和匿名)包含解决本线程中概述的问题所需的所有解决方法,并且适用于 IE (8.0.6001)、FF(3.5.9) 和 Chrome (5.0.6)。 375.55 测试版)。尚未在旧版本浏览器下进行测试。非常感谢所有回复的人。

我还应该补充一点,我需要确保服务器响应需要包括:

Response.ContentType = "text/xml" ;

才能与 IE 一起使用。 FF 不介意 ContentType 是否为 text/HTML,但 IE 却咳嗽了。

创建 XMLHTTP 请求的代码:

function GetXMLHTTPRequest () 
{
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] ; //activeX versions to check for in IE
if (window.ActiveXObject)  //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
    {
    for (var i=0; i < activexmodes.length ; i++)
        {
        try
            {
            return new ActiveXObject(activexmodes[i]) ;
            }
        catch (e)
            {    //suppress error
            }
        }
    }
 else if (window.XMLHttpRequest) // if Mozilla, Safari etc
    {
    return new XMLHttpRequest () ;
    }
 else
    {
    return (false) ;
    }
}

返回记录节点文本值的代码:

function GetRecordElement (ARecordNode, AFieldName)
{
try
    {
    if (ARecordNode.getElementsByTagName (AFieldName) [0].textContent != undefined)
        {
        return (ARecordNode.getElementsByTagName (AFieldName) [0].textContent) ; // Chrome, FF
        }

    if (ARecordNode.getElementsByTagName (AFieldName) [0].text != undefined)
        {
        return (ARecordNode.getElementsByTagName (AFieldName) [0].text) ;  //  IE
        }

    return ("unknown") ;    
    }
catch (Exception)
    {
    ReportError ("(GetRecordElement): " + Exception.description) ;
    }
}

执行 AJAX 请求的代码:

function GetRecord (s)
{
try 
    {
    ReportStatus ("") ;

    var xmlhttp = GetXMLHTTPRequest () ;
    if (xmlhttp)
        {
        xmlhttp.open ("GET", "blahblah.com/AJAXget.asp?...etc", true) ;

        if (xmlhttp.overrideMimeType) 
            {
            xmlhttp.overrideMimeType("text/xml") ;
            }
        xmlhttp.setRequestHeader ("Content-Type", "text/xml; charset=\"utf-8\"") ; 

        xmlhttp.onreadystatechange = function () 
            {
            if (xmlhttp.readyState == 4) 
                {
                if (xmlhttp.responseXML != null)
                    {
                    var xmlDoc = xmlhttp.responseXML;                
                    var ResultNodes = xmlDoc.getElementsByTagName ("Result") ;
                    if (ResultNodes != null)
                        {
                        var PayloadNode = xmlDoc.getElementsByTagName ("Payload") ;
                        if (PayloadNode != null)
                            {
                            var ResultText = ResultNodes [0].firstChild.nodeValue ;
                            if (ResultText == "OK")
                                {
                                ReportStatus (ResultText) ;
                                var RecordNode  = PayloadNode [0].firstChild ;
                                if (RecordNode != null)
                                    {
                                    UpdateRecordDisplay (RecordNode) ; // eventually calls GetRecordElement 
                                    }
                                else
                                    {
                                    ReportError ("RecordNode is null") ;
                                    }
                                }
                            else
                                {
                                ReportError ("Unknown response:" + ResultText) ;
                                }             
                            }    
                        else
                            {
                            ReportError ("PayloadNode is null") ;
                            }
                        }    
                    else
                        {
                        ReportError ("ResultNodes is null") ;
                        }
                    }
                else   
                    {
                    ReportError ("responseXML is null") ;
                    }
                }    
            else
                {  
                ReportStatus ("Status=" + xmlhttp.readyState) ;
                }
            }    

        ReportStatus ("Requesting data ...") ;
        xmlhttp.send (null) ;
        }
    else
        {
        ReportError ("Unable to create request") ;
        }        
    }
catch (err)
    {
    ReportError ("(GetRecord): " + err.description) ;
    }
}

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:

Response.ContentType = "text/xml" ;

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:

function GetXMLHTTPRequest () 
{
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] ; //activeX versions to check for in IE
if (window.ActiveXObject)  //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
    {
    for (var i=0; i < activexmodes.length ; i++)
        {
        try
            {
            return new ActiveXObject(activexmodes[i]) ;
            }
        catch (e)
            {    //suppress error
            }
        }
    }
 else if (window.XMLHttpRequest) // if Mozilla, Safari etc
    {
    return new XMLHttpRequest () ;
    }
 else
    {
    return (false) ;
    }
}

Code to return the text value of a record node:

function GetRecordElement (ARecordNode, AFieldName)
{
try
    {
    if (ARecordNode.getElementsByTagName (AFieldName) [0].textContent != undefined)
        {
        return (ARecordNode.getElementsByTagName (AFieldName) [0].textContent) ; // Chrome, FF
        }

    if (ARecordNode.getElementsByTagName (AFieldName) [0].text != undefined)
        {
        return (ARecordNode.getElementsByTagName (AFieldName) [0].text) ;  //  IE
        }

    return ("unknown") ;    
    }
catch (Exception)
    {
    ReportError ("(GetRecordElement): " + Exception.description) ;
    }
}

Code to perform the AJAX request:

function GetRecord (s)
{
try 
    {
    ReportStatus ("") ;

    var xmlhttp = GetXMLHTTPRequest () ;
    if (xmlhttp)
        {
        xmlhttp.open ("GET", "blahblah.com/AJAXget.asp?...etc", true) ;

        if (xmlhttp.overrideMimeType) 
            {
            xmlhttp.overrideMimeType("text/xml") ;
            }
        xmlhttp.setRequestHeader ("Content-Type", "text/xml; charset=\"utf-8\"") ; 

        xmlhttp.onreadystatechange = function () 
            {
            if (xmlhttp.readyState == 4) 
                {
                if (xmlhttp.responseXML != null)
                    {
                    var xmlDoc = xmlhttp.responseXML;                
                    var ResultNodes = xmlDoc.getElementsByTagName ("Result") ;
                    if (ResultNodes != null)
                        {
                        var PayloadNode = xmlDoc.getElementsByTagName ("Payload") ;
                        if (PayloadNode != null)
                            {
                            var ResultText = ResultNodes [0].firstChild.nodeValue ;
                            if (ResultText == "OK")
                                {
                                ReportStatus (ResultText) ;
                                var RecordNode  = PayloadNode [0].firstChild ;
                                if (RecordNode != null)
                                    {
                                    UpdateRecordDisplay (RecordNode) ; // eventually calls GetRecordElement 
                                    }
                                else
                                    {
                                    ReportError ("RecordNode is null") ;
                                    }
                                }
                            else
                                {
                                ReportError ("Unknown response:" + ResultText) ;
                                }             
                            }    
                        else
                            {
                            ReportError ("PayloadNode is null") ;
                            }
                        }    
                    else
                        {
                        ReportError ("ResultNodes is null") ;
                        }
                    }
                else   
                    {
                    ReportError ("responseXML is null") ;
                    }
                }    
            else
                {  
                ReportStatus ("Status=" + xmlhttp.readyState) ;
                }
            }    

        ReportStatus ("Requesting data ...") ;
        xmlhttp.send (null) ;
        }
    else
        {
        ReportError ("Unable to create request") ;
        }        
    }
catch (err)
    {
    ReportError ("(GetRecord): " + err.description) ;
    }
}
你是年少的欢喜 2024-09-10 04:15:56

我刚刚找到了解决这个问题的方法。在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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文