Why can I not get XMLHttpRequest to work!!!

C

CES

After three days I’m totally lost… I’m trying open an XML file in IE Mozilla and Safari. I was able to get one version to work in IE and Mozilla (MAC and Windows), the code that is commented out, but not Safari or Opera I then found that the preferred way to open is by using XMLHttpRequest but I can’t get any instance to work.

I found the below code on Apples Dev Site and have seen variations on others sites however the line:

xDoc.onreadystatechange = processReqChange;

which I’ve seen in every XMLHttpRequest instance seems to cause the script to stop running but I'm sure most of the (window.XMLHttpRequest) is wrong.

As you can tell I don’t have a clue and would greatly appreciate any help or at least a pointer to a VERY SIMPLE version of code that uses XMLHttpRequest, that actualy works..., Thanks in advance. - CES


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<script type="text/javascript">
function loadXMLData(){
var xmlFileName = 'demo.xml';
var xDoc;
if(window.ActiveXObject && /Win/.test(navigator.userAgent)){
xDoc = new ActiveXObject("Microsoft.XMLDom");
xDoc.async = false;
xDoc.load(xmlFileName);
writeXMLData(xDoc);
return true;
}
/*
// This works in IE and Mozilla (Windows and MAC) but not Safari
else if(document.implementation && document.implementation.createDocument){
xDoc = document.implementation.createDocument("","", null);
xDoc.load(xmlFileName);
xDoc.onload = function(){
writeXMLData(xDoc);
}
return true;
}
*/
else if (window.XMLHttpRequest) {

xDoc = new XMLHttpRequest();
xDoc.onreadystatechange = processReqChange; // If this line is here it causes the code to stop working
xDoc.open("GET", xmlFileName, true);
xDoc.send(null);
if (xDoc.readyState == 4) {
if (xDoc.status == 200) {
writeXMLData(xDoc);
return true;
}
}
alert("Mozilla Fork");
}
else{
return false;
}
}
</script>
</head>
<body onLoad="">
<div id="xmlInfo">
<script type="text/javascript">
var e = document.getElementById("xmlInfo");
function writeXMLData(xDoc){
var movies = xDoc.getElementsByTagName("movie");
for(var i = 0;i < movies.length;++i){
e.appendChild(document.createElement("div")).innerHTML = movies.firstChild.nodeValue;
}
}
if(! loadXMLData()){
alert("NO XML");
}
</script>
</div>

</body>
</html>
 
F

Fred Oz

CES said:
After three days I’m totally lost… I’m trying open an XML file in IE
Mozilla and Safari. I was able to get one version to work in IE and
Mozilla (MAC and Windows), the code that is commented out, but not
Safari or Opera I then found that the preferred way to open is by using
XMLHttpRequest but I can’t get any instance to work.

I found the below code on Apples Dev Site and have seen variations on
others sites however the line:

xDoc.onreadystatechange = processReqChange;

which I’ve seen in every XMLHttpRequest instance seems to cause the
script to stop running but I'm sure most of the (window.XMLHttpRequest)
is wrong.

As you can tell I don’t have a clue and would greatly appreciate any
help or at least a pointer to a VERY SIMPLE version of code that uses
XMLHttpRequest, that actualy works..., Thanks in advance. - CES
[...]

Try this (no guarantees, however some good comments have been
reported...):

<URL:http://www.webpasties.com/xmlHttpRequest/>
 
M

Martin Honnen

CES said:
After three days I’m totally lost… I’m trying open an XML file in IE
Mozilla and Safari. I was able to get one version to work in IE and
Mozilla (MAC and Windows), the code that is commented out, but not
Safari or Opera I then found that the preferred way to open is by using
XMLHttpRequest but I can’t get any instance to work.

If all you want to do is load an XML document from a URL to have a
scriptable XML DOM document then for best cross browser support use
XMLHttpRequest in Mozilla, Safari 1.2, latest Konqueror I think, Opera
8.00 Beta, and Microsoft.XMLHTTP in IE 5 and later:
<http://www.faqts.com/knowledge_base/view.phtml/aid/6826/fid/616>

If you want to exchange XML data between client and server:
<http://www.faqts.com/knowledge_base/view.phtml/aid/17226/fid/616>
 
C

CES

Martin,
Thank you...

I settled on the below script before I say your post but the two URL you provided will help me in the future!!! Thanks in your effort- CES


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>

<script type="text/javascript">
function loadXMLData(){
var xmlhttp=false;
var xmlFileName = "demo.xml";

try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e){
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
xmlhttp.open("GET", xmlFileName,true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4){
if (xmlhttp.status == 200){
writeXMLData(xmlhttp);
return true;
}
else{
alert("There was a problem retrieving the XML data:\n" + xmlhttp.statusText);
}
}
}
xmlhttp.send(null);
}

function writeXMLData(xmlData){
//xmlhttp.responseXML returns XML
var y = xmlData.responseXML;
var x = y.getElementsByTagName('movie').item(0).firstChild.data;

/*
//xmlhttp.responseText returns a string that can be split into an Array
// This Dosent work the way I need
var y = xmlData.responseText.split(",");
var x = y[0];
*/

document.getElementById("xmlInfo").appendChild(document.createElement("div")).innerHTML = x;
}
</script>
</head>
<body onLoad="loadXMLData()">
<div id="xmlInfo">
</div>

</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,840
Latest member
BrendanG78

Latest Threads

Top