views:

977

answers:

2

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?

+1  A: 

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;
jeanreis
Well that cured that one, but IE now reports Object requiredat this line: var xmlDoc = xmlhttp.responseXML.documentElement ; if (xmlhttp.responseXML.xml) var xmlDoc = xmlhttp.responseXML.xml; else var xmlDoc = xmlhttp.responseXML; var ResultNodes = xmlDoc.getElementsByTagName ("Result") ; if (ResultNodes != null) { var PayloadNode = xmlDoc.getElementsByTagName ("Payload") ; <-- error if (PayloadNode != null)Code OK on FF, Chrome
and how do you stop StackOverflow wrapping lines in comments? - (code looked fine when I typed it in!)
Oh, you have to remove this line:var xmlDoc = xmlhttp.responseXML.documentElement;right before the if (xmlhttp.responseXML.xml) line.
jeanreis
A few lines further on.. var ResultNodes = xmlDoc.getElementsByTagName ("Result") ; if (ResultNodes != null) { var PayloadNode = xmlDoc.getElementsByTagName ("Payload") ; if (PayloadNode != null) { var ResultText = ResultNodes [0].firstChild.nodeValue ; <-- object required Inspection shows ResultNodes to be null
A: 

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) ;
    }
}