Click here to Skip to main content
15,909,530 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So, I wrote a JavaScript to grab an xml file from my desktop and display it on an html page. However, I now have added my xml file to a webserver (mongoose). I want to call the file from that server, but whenever I call the file from the server it dosen't work, but when I call it from my desktop it loads fine.

I want to swap
xmlhttp.open("GET","Devices.xml",false);



with
xmlhttp.open("GET","http://localhost:8080/Devices.xml",false);


Here is the code
XML
<<html>
<head>

<script type="text/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","Devices.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;


// the <Device> list
x = xmlDoc.getElementsByTagName('Device');

// make a function that extracts the attributes out of a Node
function getDeviceAttributes(dvc)
{
    var name = dvc.getAttribute("name");
    var uuid = dvc.getAttribute("uuid");
    var id   = dvc.getAttribute("id");
    return "<p>name: " + name + "<br> uuid: " + uuid + "<br> id: "+ id + "</p>";
}


// loop through the list
// assuming order doesn’t matter
var txt = '';
for (var i = x.length; i--;)
{
    txt += getDeviceAttributes(x[i]);
}

</script>
</head>

<body>


<script type="text/javascript">
//show the result on page load
window.onload = function()
{
    document.getElementById("showDevices").innerHTML = txt;
};

</script>

<div id='showDevices'></div>

</body>
</html>


Does anyone know how I can get this to work?
Posted

You are using the xmlhttp object in synchronous mode. XmlHttp is meant for asynchronous access. You'll have to register an event handler for when the response has been received from the webserver, instead of trying to immeadiately reading the response after calling send. The reson it worked locally from the file system is most likely due to that the delay is so small that you wouldn't notice it. You can use the onreadystatechanged event of the XmlHttp object.

For a simple example on asynchronous usage see here: http://de.wikipedia.org/wiki/XMLHttpRequest[^].

Better yet, use a library for your Ajax needs like jQuery[^] for instance.

Cheers!

—MRB
 
Share this answer
 
v2
Did you see whether the url "http://localhost:8080/Devices.xml[^]"

works fine in both the cases?
t looks like the source "Device.xml" is not in correct position( like as you wanted to access the file from this location: http://localhost:8080/Devices.xml[^])

Check the path.

And again if the "localhost..." is hardcoded, the code would not work except localhost or your local computer. You should you relative path.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 11-Jul-11 9:25am    
Moved from OP's answer:

I added both the HTML file and the XML file to the server. It still gives me "undefined". I am lost.
If you have only moved the XML file to the web server but not the HTML file, then the problem is that you are attempting cross-site scripting. The browser will not allow you to load data from a different domain.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 11-Jul-11 9:25am    
Moved from OP's answer:

I added both the HTML file and the XML file to the server. It still gives me "undefined". I am lost.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900