470,870 Members | 1,447 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

Total N00b question

Hi all,

I pre-apologize for the level of stupidity that this message will
contain. I nearly guarantee that your IQ will be lowered by the end of
this message.

Me and a co-worker (I only bring him into this to try to divide the
stupidity in half, thus making us each appear only half as dumb as we
could, oh wait, will we come off twice as dumb since there are two of
us, maybe this plan is backfiring on me)...

We are working a very simple AJAX example that we downloaded from the
Mozilla Dev Center
(http://developer.mozilla.org/en/docs...tting_Started). Using the
presented example everything comes out dandy. However the example loads
a static XML file called test.xml.

Obviously a static XML file isn't too dynamic, so we wanted, nice and
easy to start to make the example more dynamic and dynamically generate
the XML data...

We converted the test.xml file to an ASP file...

Flat test.xml file:
<?xml version="1.0" ?>
<root>
I'm a test.
</root>

We created a test.asp file with the following:
<%
response.write( "<?xml version=""1.0"" ?>" )
response.write( "<root>" )
response.write( "Im a test" )
response.write( "</root>" )
%>

Which we imagined would have the same result. We can load test.xml or
test.asp in our browser directly and both look the same in our browser.
However when we load our Ajax HTML page and click the button to load
our XML file, it refuses to parse the XML with the ASP generated XML.

As a sidenote, this all works fine in Firefox.

Many hours later we figured it must have had something to do with the
headers that IIS and ASP were passing to the browser. So we loaded
Apache + PHP 5. We created a test.php file which outputs our simple XML
document. Again loading up test.php directly is fine, but within the
Ajax app it refuses to parse (in Internet Explorer, again Firefox
handles it fine). We also added header() php function calls before we
pass the XML data to force it to be text/xml data type, still no go. I
changed Apache to pass .xml files into PHP and pass along to the
client, in case the Ajax was not accepting of a .php file passing an
XML file... still no go..

Ok.. so there's a day in the bank.. the longest time ever to create a
"Hello World" application (I hope my boss isn't reading this since I am
going to be pink slipped for being so unproductive).

We decided it must be some problem with the Types, Mime Types, all that
stuff that IE still was refusing to see our "dynamically generated" XML
file...

We rewrote our dynamic XML output using the XML DOM model... I create
a very simple document named test2.php as follows:

<?php
$doc = new DOMDocument();
$doc->formatOutput = true;

$now = time();
$developers = $doc->createElement( "time" );
$doc->appendChild( $developers );
$developers->appendChild( $doc->createTextNode($now) );

echo $doc->saveXML();
?>

My JavaScript to parse out the data looks like:

function alertContents()
{
if (http_request.readyState == 4)
{
if (http_request.status == 200)
{
alert(http_request.responseText);
var xmldoc = http_request.responseXML;
var root_node = xmldoc.getElementsByTagName('time').item(0);
if( root_node )
alert(root_node.firstChild.data);
else
{
alert( 'Poop the bed' );
}
}
else
{
alert('There was a problem with the request.');
}
}
}

We always see "Poop the Bed" in IE and it works fine in Firefox.

That's officially the end of the rope... We're going to go home now and
drown ourselves in our mediocrity and hope to return to work Monday and
read the mockings of our fellow developers here in this public forum,
and maybe an idea or two on what the heck we are doing wrong...

Thanks!

Matt

Jan 27 '06 #1
3 1555
Matt Fuerst wrote:
We are working a very simple AJAX example that we downloaded from the
Mozilla Dev Center
No AJAX application is simple :-)

[...] We created a test.asp file with the following:
<%
response.write( "<?xml version=""1.0"" ?>" )
response.write( "<root>" )
response.write( "Im a test" )
response.write( "</root>" )
%>

Which we imagined would have the same result. We can load test.xml or
test.asp in our browser directly and both look the same in our browser.
However when we load our Ajax HTML page and click the button to load
our XML file, it refuses to parse the XML with the ASP generated XML.

As a sidenote, this all works fine in Firefox.
Yep. As per usual.

[...] We decided it must be some problem with the Types, Mime Types, all that
stuff that IE still was refusing to see our "dynamically generated" XML
file...


Could be. FAQ. http://xml.silmaril.ie/developers/serversoftware/

///Peter
Jan 28 '06 #2

You're not alone Matt. I've been programming for 10+ years in several
different languages and just attempted to do exactly what you were
trying to do and came across a little frustration of my own. This
should work for IE. Load these files in the same directory and you
should get alerted when you go to test.asp. Hope this helps.

D.
file: test2.asp
-------------------------------------------
<%
response.buffer=true
response.contentType = "text/xml"
%>
<root>
<value>
<nm>I'm a test.</nm>
<party>Darrell</party>
</value>
<value>
<nm>test 2</nm>
<party>Robinson </party>
</value>
</root>
--------------------------------------------

file: test.asp
--------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY onload="makeRequest('test2.asp');">
<script type="text/javascript" language="javascript">

var http_request = false;

function makeRequest(url) {

http_request = false;

if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
// See note below about this line
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new
ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}

if (!http_request) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('GET', url, true);
http_request.send(null);

}

function alertContents() {

if (http_request.readyState == 4) {
if (http_request.status == 200) {
var xmldoc = http_request.responseXML;
//var root_node = xmldoc.getElementsByTagName('root').item(0);
//alert(root_node.firstChild.data);
err = xmldoc.parseError;
if (err.errorCode != 0) {
alert("Error: " + err.reason);
} else{
var xml = xmldoc.getElementsByTagName('root').item(0);
var y =
xmldoc.getElementsByTagName('root').item(0).getEle mentsByTagName('value').length-1;
for(var x=0;x<=y;x++){
alert(xml.getElementsByTagName('value').item(x).ge tElementsByTagName('nm').item(0).firstChild.data)
alert(xml.getElementsByTagName('value').item(x).ge tElementsByTagName('party').item(0).firstChild.dat a)
} }
} else {
alert('There was a problem with the request.');
}
}

}
</script>
<span
style="cursor: pointer; text-decoration: underline"
onclick="makeRequest('test2.asp')">
Make a request
</span>

</BODY>
</HTML>
---------------------------------------------------
--
dtrobinson
------------------------------------------------------------------------
dtrobinson's Profile: http://techiegroups.com/member.php?userid=64
View this thread: http://www.techiegroups.com/showthread.php?t=100596

Feb 7 '06 #3
vioLab
1
Hey,
I just wanted to post a quick comment here in case anyone else googles this thread as I came across it yesterday when experiencing the same problem but neither of the above solutions helped. In the end the problem was that there was a line break between the end of my asp, ie. %> and the xml declaration - a very simple thing but it was frustrating my ajax efforts for hours. Might help someone else...
Apr 28 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Anupam Kapoor | last post: by
1 post views Thread by newgenre | last post: by
6 posts views Thread by Charles | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.