admin管理员组

文章数量:1415119

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:

<ZipCodes>
<results>
    <city>Chicago</city>
    <state>IL</state>
    <timezone>-6</timezone>
</results>

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 ing in on the network response when I look at the debug in Chrome.

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:

<ZipCodes>
<results>
    <city>Chicago</city>
    <state>IL</state>
    <timezone>-6</timezone>
</results>

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 ing in on the network response when I look at the debug in Chrome.

Share Improve this question asked Dec 6, 2014 at 12:02 Charles KielCharles Kiel 1272 gold badges2 silver badges8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

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 plete.

Currently you're asking for data that hasn't been transmitted yet.

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

Then the output when viewed from browser source will be this: 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.

本文标签: javascriptCannot read property 39getElementsByTagName39 of nullStack Overflow