...

View Full Version : xmlHttpRequest Object not retrieving XML File



Ahlahn
01-11-2011, 05:58 PM
Hey everyone,

I'm testing the code for ajax by trying to retrieve a simple xml document. I placed both the xml file and the html file on my desktop, but it's not working. I don't think there's anything wrong with the code- I found it on w3schools and modified it to retrieve the new file I created. Please take a look.

HTML FILE


<!DOCTYPE html PUBLIC "-//W3C
//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/
xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>Insert Title Here</title>
<script language="JavaScript" type="text/javascript">
<!--
function loadXMLDoc()
{
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)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","info.xml",true);
xmlhttp.send();
}
//-->
</script>

<style type="text/css">
</style>

</head>
<body>

<div id="myDiv"><h2>I will attempt to change this text using AJAX!</h2></div>
<button type="button" onClick="loadXMLDoc()">Change Content PLZ</button>


</body>
</html>


XML File


<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<message>Hello World!</message>
</note>



Thanks!

devnull69
01-11-2011, 06:08 PM
HTTP requests need a webserver as a destination, so without a webserver (even on localhost) it is not possible to retrieve files.

DaveyErwin
01-11-2011, 06:29 PM
HTTP requests need a webserver as a destination, so without a webserver (even on localhost) it is not possible to retrieve files.

Ihave the following code in an html file
on my desktop (windows xp)...


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>An XHTML 1.0 Strict standard template</title>
<meta http-equiv="content-type"
content="text/html;charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
alert('!IE')
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}
if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}
}
xmlhttp.open("GET","test.xml" ,true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
alert(xmlhttp.getAllResponseHeaders())
alert(xmlhttp.responseXML)
alert(xmlhttp.responseText)

var root = xmlhttp.responseXML.getElementsByTagName('*');
alert(root.length)
}
}
xmlhttp.send(null)
//]]>
</script>
</head>
<body>
<p> Your HTML content here </p>
</body>
</html>

also on my desktop i have an xml file
named test.xml

try it out
works for me

devnull69
01-11-2011, 07:18 PM
Wow ok so the problem must be somewhere else ... I keep learning every day :thumbsup:

@DaveyErwin: Are you able to detect the problem then?

DaveyErwin
01-11-2011, 07:37 PM
there is a lot of bad ajax around
even at w3school
try like this ...



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type"
content="text/html;charset=utf-8" />
<title>An XHTML 1.0 Strict standard template</title>
<meta http-equiv="content-type"
content="text/html;charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
alert('!IE')
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}
if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}
}
xmlhttp.open("GET","test.xml" ,true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
document.getElementById('dsp').innerHTML=xmlhttp.responseText;
}
}
//]]>
</script>
</head>
<body>
<p id="dsp">… This text will change …</p>
<p><input type="button" onclick="xmlhttp.send(null)"/></p>
</body>
</html>


keyword:ajax;
keyword:Msxml2.XMLHTTP;
keyword:Microsoft.XMLHTTP;
keyword:XMLHttpRequest;
keyword:read from file system;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum