Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    cant fetch xml with ajax

    Hi!

    I am trying to fetch information from a xml-file, I am using this code and have no Idea why it is not working:

    Anyone who can help me out, I would be very greatful!

    the code is:

    <html>
    <head>
    <script type="text/javascript">
    function loadXMLDoc()
    {
    var xmlhttp;
    var txt,x,i;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    xmlDoc=xmlhttp.responseXML;
    txt="";
    x=xmlDoc.getElementsByTagName("Layer");
    for (i=0;i<x.length;i++)
    {
    txt=txt + x[i].childNodes[0].nodeValue + "<br />";
    }
    document.getElementById("myDiv").innerHTML=txt;
    }
    }
    xmlhttp.open("GET","http://atlas.gc.ca/cgi-bin/atlaswms_en?VERSION=1.1.1&REQUEST=Getcapabilities&SERVICE=wms",true);
    xmlhttp.send();
    }
    </script>
    </head>

    <body>

    <h2>Layers:</h2>
    <div id="myDiv"></div>
    <button type="button" onclick="loadXMLDoc()">Get Layers</button>

    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Is this XML file coming from the same server (domain) as the file with the javascript? Otherwise you might be facing a cross-site request which violates the Same Origin Policy.

  • #3
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thank you, the problem was the cross-origin...

    I set it up on a server and it worked

  • #4
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    could you please explain the cross origin problem?
    I am trying to access the XML doc from http://atlas.gc.ca /cgi-bin/atlaswms_en?VERSION=1.1.1&request=Getcapabilities&service=wms from a html file i my system.
    <html>
    <body>
    <h1>W3Schools Internal Note</h1>
    <div>
    <b>To:</b> <span id="to"></span><br />
    <b>From:</b> <span id="from"></span><br />
    <b>Message:</b> <span id="message"></span>
    </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");
    }
    var url="http://atlas.gc.ca/cgi-bin/atlaswms_en?VERSION=1.1.1&request=Getcapabilities&service=wms";
    xmlhttp.open("GET",url,false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;
    document.write(xmlDoc.getElementsByTagName("Layer")[0].getElementsByTagName("Name")[0].childNodes[0].nodeType);

    </script>

    </body>
    </html>

    You mentioned about setting client on server. Could u please explain??

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,460
    Thanks
    11
    Thanked 600 Times in 580 Posts
    you can only fetch your own pages/responses.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    You can only fetch pages from the same domain, subdomain AND protocol as those of the current page.

    Possible solutions:
    1. Server side proxy (e.g. PHP proxy) on your domain that accesses the cross-domain site and routes the output to the client 1-to-1
    2. JSONP


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •