...

View Full Version : cant fetch xml with ajax



paulinho
03-10-2011, 05:33 PM
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>

devnull69
03-11-2011, 08:10 AM
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.

paulinho
03-11-2011, 02:06 PM
thank you, the problem was the cross-origin...

I set it up on a server and it worked

shivareddy
08-10-2011, 08:25 PM
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??

rnd me
08-16-2011, 12:11 AM
you can only fetch your own pages/responses.

devnull69
08-16-2011, 08:35 AM
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 (http://en.wikipedia.org/wiki/JSONP)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum