问题
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:
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;
回答2:
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) ;
}
}
回答3:
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.
来源:https://stackoverflow.com/questions/2901077/null-is-null-or-not-an-object-error-in-ie-javascript