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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2007
    Location
    Dublin (Ireland)
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cannot access local file, only file on web-server

    I'm following the XML DOM tutorial
    at <http://www.w3schools.com/dom/dom_intro.asp>,
    and in particular trying the example below.

    Basically, this works fine with test.html and note.xml
    on my apache http server,
    but not when I point my firefox browser
    to a local file test.html, and give a local file note.xml as URL
    (either as 'note.xml' or './ note.xml' or 'file:///home/tim/.../note.xml').

    I get the message: "Problem retrieving XML data".

    I guess this is a very simple question about URLs.
    Apologies if it is insultingly simple;
    I'm not very knowledgeable in this area.

    [tim@carrie ~]$ cat test.html
    ---------------------------------------------------------
    <html>
    <head>
    <script type="text/javascript">
    var xmlhttp

    function loadXMLDoc(url)
    {
    xmlhttp=null
    if (window.XMLHttpRequest)
    {
    xmlhttp=new XMLHttpRequest()
    }
    if (xmlhttp!=null)
    {
    xmlhttp.onreadystatechange=state_Change
    xmlhttp.open("GET",url,true)
    xmlhttp.send(null)
    }
    else
    {
    alert("Your browser does not support XMLHTTP.")
    }
    }

    function state_Change()
    {
    // if xmlhttp shows "loaded"
    if (xmlhttp.readyState==4)
    {
    // if "OK"
    if (xmlhttp.status==200)
    {
    alert("XML data OK")
    document.getElementById('A1').innerHTML=xmlhttp.status
    document.getElementById('A2').innerHTML=xmlhttp.statusText
    document.getElementById('A3').innerHTML=xmlhttp.responseText
    }
    else
    {
    alert("Problem retrieving XML data:" + xmlhttp.statusText)
    }
    }
    }
    </script>
    </head>

    <body onload="loadXMLDoc('file:///home/tim/COMPUTING/HTML/note.xml')">
    <h2>Using the HttpRequest Object</h2>

    <p><b>status:</b>
    <span id="A1"></span>
    </p>

    <p><b>status text:</b>
    <span id="A2"></span>
    </p>

    <p><b>response:</b>
    <br><span id="A3"></span>
    </p>

    </body>
    </html>
    ---------------------------------------------------------
    [tim@carrie]$ cat note.xml
    ---------------------------------------------------------
    <note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
    </note>
    --------------------------------------------------------

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    XMLHttpRequests only work if u have a server this is done mainly to prevent malicious scripts from accessing resources from other domains


  •  

    Posting Permissions

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