Hello. I have this code
var request;
if(window.XMLHttpRequest){
request = new XMLHttpRequest()
}else{
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.open('GET', './xml/txml.xml');
request.onreadystatechange = function(){
if((request.readyState===4) && (request.status===200)){
console.log(request.readyState);
console.log(request.status);
console.log(request.responseText);
var xmlData = request.responseXML;
var textOutput = xmlData.getElementsByTagName("bisCard")[0];
var y = textOutput.childNodes[0];
alert(y);
}
}//end request.onreadystatechange
request.send();
The ready state is 4, status 200. the
console.log(request.responseText);
gives me this in the console
<?xml id="xmldata1" version="1.0" encoding="UTF-8"?>
<bisCard>
<name>joe Bimbo</name>
<phone type="mobile">503 123.1234</phone>
<phone type="work">503 123.1234</phone>
<phone type="fax">503 123.1234</phone>
<email>joeBimbo@joeBimbo.com</email>
</bisCard>
but I I get the âUncaught TypeError: Cannot read property âgetElementsByTagNameâ of nullâ error.
Why is it not picking up on the data available? Thx
What does this show?
console.log(request.responseXML);
1 Like
Is txml.xml returning a
Content-Type: application/xml
header?
Hello @Mittineague this is the entire .xml
<?xml id="xmldata1" version="1.0" encoding="UTF-8"?>
<bisCard>
<name>joe Bimbo</name>
<phone type="mobile">503 123.1234</phone>
<phone type="work">503 123.1234</phone>
<phone type="fax">503 123.1234</phone>
<email>joeBimbo@joeBimbo.com</email>
</bisCard>
Yes, that is the text of the file.
What Iâm talking about is the HTTP header.
Most set-ups default to âtext/htmlâ
If you arenât sending the âapplication/xmlâ header, then I guessing âtextâ is used instead of the xml that you want.
weirdâŚthis worked.
var request;
if(window.XMLHttpRequest){
request = new XMLHttpRequest()
}else{
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.open('GET', './xml/txml.xml');
request.onreadystatechange = function(){
if((request.readyState===4) && (request.status===200)){
console.log(request.readyState);
console.log(request.status);
console.log(request.responseXML);
console.log(request.responseText);
console.log(request.responseXML.getElementsByTagName('name')[0]);
var xmlData = request.responseXML;
var name = xmlData.getElementsByTagName('name')[0];
$("#selectPnl1").append(name);
}
}//end request.onreadystatechange
request.send();
I finally got a result & it show on the .php page. No header there requiring the application/xml. Frankly puzzled (but donât call me frank).
Hmmm. Shirley you must joking
I had a look at what my localhost httpd.conf had for âtypesâ it uses the âmime.typesâ file, and that does have application/xml xml
in it.
Iâm guessing the âalertâ I hadnât noticed before had something to do with the problem.
1 Like
Good catch on the Shirley (that was a fun movie.Surpassed by young frankenstein of course.)
so recon it was a typo? Glad it works now. Thx
system
Closed
June 1, 2016, 4:57am
10
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.