Total N00b question

M

Matt Fuerst

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/AJAX:Getting_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
 
P

Peter Flynn

Matt said:
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
 
D

dtrobinson

You're not alone Matt. I've been programming for 10+ years in severa
different languages and just attempted to do exactly what you wer
trying to do and came across a little frustration of my own. Thi
should work for IE. Load these files in the same directory and yo
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 = ne
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).getElementsByTagName('value').length-1;
for(var x=0;x<=y;x++){
alert(xml.getElementsByTagName('value').item(x).getElementsByTagName('nm').item(0).firstChild.data)
alert(xml.getElementsByTagName('value').item(x).getElementsByTagName('party').item(0).firstChild.data)
} }


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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top