how to fetch a xml file from different site

Discussion in 'Javascript' started by Jesse Woo, Mar 16, 2006.

  1. Jesse  Woo

    Jesse Woo Guest

    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?
     
    Jesse Woo, Mar 16, 2006
    #1
    1. Advertising

  2. Jesse Woo wrote:

    > 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.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Mar 16, 2006
    #2
    1. Advertising

  3. Jesse  Woo

    Randy Webb Guest

    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:


    <snip>

    > 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.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Mar 16, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ted
    Replies:
    1
    Views:
    566
    DM McGowan II
    Jul 3, 2004
  2. aftab
    Replies:
    0
    Views:
    392
    aftab
    May 15, 2004
  3. Phil Winstanley [Microsoft MVP]

    Re: Fetch data from two different database -Datagrid control

    Phil Winstanley [Microsoft MVP], May 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    540
    aftab
    May 15, 2004
  4. Phil Winstanley [Microsoft MVP]

    Re: Fetch data from two different database -Datagrid control

    Phil Winstanley [Microsoft MVP], May 15, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    423
    Phil Winstanley [Microsoft MVP]
    May 15, 2004
  5. Erik Wasser
    Replies:
    5
    Views:
    465
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page