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?

3
  • 2
    w3.org/Security/wiki/Same_Origin_Policy Commented Dec 26, 2011 at 10:25
  • 1
    As John_doe point out you can call Ajax on different server. But you can use jsonp for this en.wikipedia.org/wiki/JSONP Commented Dec 26, 2011 at 11:29
  • Haha, n00b mistake. Thank you very much. Commented Dec 26, 2011 at 16:16

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

That explains why I never find an example with cross domains. I really apreciate your help guys.
If you are satisfied with some answer, its good that you accept it :)
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: stackoverflow.com/questions/11778074/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.