1

I am working in an iPhone app in HTML. It needs to retrieve content from an xml stored in a remote server and show its content in a list.

I have already achieved this when the xml file is stored in the same server with this code:

    <body>
        <div id="container"> 
            <div id="header">
                <h1><a href="./">BurgerFast</a></h1>
                <p>Menú</p>
                <div>
                    <div id="nav">
                        <ul>
                            <span id="lista"></span>
                        </ul>
                    </div>
                    <div id="footer">
                        <ul>
                            <li><a href="/assets/cl.png">Acerca de</a></li>
                            <li><a href="/assets/cl.png">Ayuda</a></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <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","assets/note.xml",false);
        xmlhttp.send();
        xmlDoc=xmlhttp.responseXML;

        var item = xmlDoc.getElementsByTagName("item")

        var myElems = "";
        for(i = 0 ; i < item.length ; i++){
            myElems = myElems + "<li><a>" + item[i].getElementsByTagName("name")[0].childNodes[0].nodeValue + "</li></a>";
        }

        document.getElementById("lista").innerHTML =  myElems;


        </script>

    </body>

So far, no problem. As the file is hosted in the same server works nicely, but I want to be able to load xml from different servers but mine. When I change the line:

xmlhttp.open("GET","assets/note.xml",false);

and leave itlike this:

xmlhttp.open("GET","http://173.236.56.146/~crayonli/xml/note.xml", true);

it tells me everytime the xmldoc is null, I don't get it, it's the same damn xml.

I just cannot see where's my mistake. Can somebody tell?

Ivancho
  • 141
  • 3
  • 14

1 Answers1

1

You cannot use XmlHttp across different domains. Cross domains are not supported. For achieving it you can use a proxy mechanism or just google xmlhttp cross domain...

This link might help you more XmlHttp Corss Domain Proxy

Kris
  • 8,680
  • 4
  • 39
  • 67
  • That explains why I never find an example with cross domains. I really apreciate your help guys. – Ivancho Dec 26 '11 at 16:17
  • If you are satisfied with some answer, its good that you accept it :) – Kris Dec 27 '11 at 06:07
  • you CAN use XHR across domains, but only if the server is returning JSONP. if the server only returns XML, then you need to make your own proxy. note: deploying a proxy in php is a lot easier than .NET but mind you, you need to make sure your proxy only talks to 1 server and HTML encodes any GET params etc. to prevent someone exploiting your proxy .. similar post with a PHP proxy answer: http://stackoverflow.com/questions/11778074/parsing-xml-data-from-a-remote-website – Sean Jan 07 '16 at 20:32