0

I need to display the XML data from this URL: http://api.redfoundry.com/RFBase.ashx?type=get&name=b39d11d5-505f-453e-bcfc-9d3b19dd8a61&key=2469F6F56F61976FB51FDEC878E93555

into a HTML table that will automatically load when the HTML page is visited.

So far I have got the following but it doesn't seem to work, it's come from a W3 tutorial:

<html>
    <body>
        <script>
            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","http://api.redfoundry.com/RFBase.ashx?type=get&name=b39d11d5-505f-453e-bcfc-9d3b19dd8a61&key=2469F6F56F61976FB51FDEC878E93555",false);
            xmlhttp.send();
            xmlDoc = xmlhttp.responseXML; 
            document.write("<table border='1'>");
            var x = xmlDoc.getElementsByTagName("item");
            for (var i=0; i<x.length; i++) { 
                document.write("<tr><td>");
                document.write(x[i].getElementsByTagName("placename")[0].childNodes[0].nodeValue);
                document.write("</td><td>");
                document.write(x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue);
                document.write("</td></tr>");
            }
            document.write("</table>");
        </script>
    </body>
</html>

The results that I want to show in the table are 'Item ID', placename, description, and address.

Danubian Sailor
  • 1
  • 38
  • 145
  • 223
googleyberry
  • 73
  • 3
  • 10

1 Answers1

1

Check the error console of your browser, it is probably telling that the access is denied. The same origin policy inside browsers usually prevents that an XMLHttpRequest can load data from a different origin than the one the document with the script is loaded from. So unless the server is set up to support CORS and you are using a browser supporting that too you can't load a document with XMLHttpRequest from the origin http://api.redfoundry.com.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Okay, Thanks for noticing that. Do you know what I would need to do to get something like this to work without altering the server (It's 3rd party, I dont have control over it). – googleyberry May 25 '13 at 14:45
  • Try jsonp, see this: http://stackoverflow.com/questions/2558977/ajax-cross-domain-call – ronnyfm May 25 '13 at 15:03
  • You have the options outlined in the linked post (setting up a proxy on your server or using JSONP) – Martin Honnen May 25 '13 at 16:42