how to fetch a xml file from different site


J

Jesse Woo

Hi, I don't know javasript much. But I know js is the right solution to
my need. I have an xml file (http://example1.com/list.xml) that will be
updated from time to time. I want to give following js code to many
sites (http://example2.com http://example3.com ...) to get newest data
in http://example1.com/list.xml:

code that will be in http://example2.com http://example3.com ... :
// -- code starts here
<script src="http://example1.com/js/fetchsite.js"
type="text/javascript">
</script>
<script type="text/javascript">
ajaxRead('http://example1.com/list.xml');
</script>
// -- code ends here

http://example1.com/js/fetchsite.js :
// -- code starts here
function ajaxRead(file) {
var xmlObj = null;
if(window.XMLHttpRequest) {
xmlObj = new XMLHttpRequest();
} else if(window.ActiveXObject) {
xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
} else {
return;
}
xmlObj.onreadystatechange = function() {
if(xmlObj.readyState == 4){
processXML(xmlObj.responseXML);
}
}
xmlObj.open ('GET', file, true);
xmlObj.send ('');
}
function processXML(obj) {
...
}
// -- code ends here

I googled, it seems that XMLHttpRequest get can't fetch file from a
different site, is that right? Then how can I work around this
limitation?
 
Ad

Advertisements

M

Martin Honnen

Jesse said:
I googled, it seems that XMLHttpRequest get can't fetch file from a
different site, is that right? Then how can I work around this
limitation?

Only on the server, there you can (usually) fetch data from other
servers and that way if your HTML document with the script comes from
http://example.com/ and wants to access e.g. http://example.org/file.xml
then you need to make a request alike
http://example.com/fetch?url=http://example.org/file.xml
so that your server side script (named 'fetch' in the above example)
accesses the other server example.org to receive the file and return it
to your client-side script.
 
Ad

Advertisements

R

Randy Webb

Jesse Woo said the following on 3/16/2006 4:09 AM:
Hi, I don't know javasript much. But I know js is the right solution to
my need. I have an xml file (http://example1.com/list.xml) that will be
updated from time to time. I want to give following js code to many
sites (http://example2.com http://example3.com ...) to get newest data
in http://example1.com/list.xml:

I googled, it seems that XMLHttpRequest get can't fetch file from a
different site, is that right? Then how can I work around this
limitation?

<URL: http://borkweb.com/story/look-ma-cross-domain-scripting >

You do it by creating a script element, dynamically, and setting its src
property to the data returned.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top