...

View Full Version : Cannot access local file, only file on web-server



gayleard
02-13-2007, 03:51 PM
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>
--------------------------------------------------------

shyam
02-13-2007, 04:41 PM
XMLHttpRequests only work if u have a server this is done mainly to prevent malicious scripts from accessing resources from other domains



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum