|
|
AJAX Question
|
|
|
|
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status:
Offline
|
|
I'm working on a site that uses AJAX pretty extensively. Up until now, the way I've always done it is to have the javascript sent the HTTP request to a PHP "page" that uses the $_GET variable to return the correct result (usually drawn from a database) as simple text/HTML. Once I've done that I can just set the innerHTML of some div on my page to the responseText of the xmlhttp object.
Now, however, I want to be set the data in three different div tags with a single HTTP request. It seems like I should be able to do this easily enough by having the response from the PHP file be XML. However no matter what I've tried the responseXML is 'undefined', and it doesn't seem to let me use any XML manipulations like getElementsByTagName on the responseText.
Does anyone know if it is possible to have the PHP script return something that will be recognized in the responseXML, or am I going to have to resort to some sort of text processing to extract the different sets of data from the responseText string?
Thanks.
|
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status:
Offline
|
|
Well, as long as it's valid, well-formed XML, responseXML should work. Do you have an example of what you're returning as your XML?
|
Just who are Britain? What do they? Who is them? And why?
Formerly Black Book
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Nov 2001
Location: Are Eye
Status:
Offline
|
|
sounds like you'll have to resort to some sort of text processing.
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Is the mime type set to text/xml?
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status:
Offline
|
|
Originally Posted by Chris O'Brien
Well, as long as it's valid, well-formed XML, responseXML should work. Do you have an example of what you're returning as your XML?
Argh, I need to pay more attention... Got side-tracked from that project, but now I'm back.
Anyway, this is an example of the XML it's returning:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<RESPONSE>
<COLUMNS>
<COLUMN>
<NAME>Name</NAME>
<WIDTH>50%</WIDTH>
</COLUMN>
<COLUMN>
<NAME>Birthday</NAME>
<WIDTH>45%</WIDTH>
</COLUMN>
</COLUMNS>
<ENTRIES>
</ENTRIES>
<BOTTOM>
</BOTTOM>
</RESPONSE>
And here's the code I used to get at that:
Code:
var response = xmlHttp.responseText;
alert(response);
It gives me nothing if I try to use xmlHttp.responseXML or xmlHttp.responseXML.documentElement.
|
|
|
|
|
|
|
|
|
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status:
Offline
|
|
Originally Posted by Chuckit
Is the mime type set to text/xml?
An interesting question. It hadn't occurred to me that I might need to do that when echoing the XML from PHP. Here's the code that generates it:
Code:
$response ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<RESPONSE>
<COLUMNS>
";
foreach($columns as $column) {
$response .= " <COLUMN>\n";
$response .= " <NAME>$column->name</NAME>\n";
$response .= " <WIDTH>$column->width</WIDTH>\n";
$response .= " </COLUMN>\n";
}
$response .= " </COLUMNS>
<ENTRIES>
</ENTRIES>
<BOTTOM>
</BOTTOM>
</RESPONSE>";
echo $response;
(It's really annoying how indentation isn't preserved. Good thing I'm not doing this in Python...)
|
|
|
|
|
|
|
|
|
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status:
Offline
|
|
Still haven't been able to get this working. Found an AJAX tutorial explaining exactly how this should work here, but the method they say to use is exactly what I was already doing. And the script they use as a demo also doesn't work for me (and appears to be for the same reason).
Anyone have any thoughts?
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Jan 2002
Location: Live at the BBQ
Status:
Offline
|
|
This site wouldn't be using sessions to restrict user access by any chance, would it? I had a similar problem with adding AJAX functionality to a site... turns out I forgot to carry the current session over to the php script that was fetching the data from the db.
|
"Bill Gates can't guarantee Windows... how can you guarantee my safety?"
-John Crichton
|
|
|
|
|
|
|
|
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status:
Offline
|
|
Originally Posted by himself
This site wouldn't be using sessions to restrict user access by any chance, would it? I had a similar problem with adding AJAX functionality to a site... turns out I forgot to carry the current session over to the php script that was fetching the data from the db.
Nope, nothing like that.
I did find this tutorial that's using the exact same method as I am. I assume they know what they're doing and that their example should work, however the example script exhibits exactly the same problem that mine does. It doesn't work in either Safari or Camino. Can anyone else get it to work in any browser?
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Jul 2001
Location: Chicago, IL USA
Status:
Offline
|
|
Based on what I've seen in this thread it's not entirely clear if you've set the content-type in php.
I've found that I need to put this:
Code:
header("Content-type: text/xml");
into my php scripts (before any other print/echo commands of course).
|
|
|
|
|
|
|
|
|
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status:
Offline
|
|
Ah, ok. I'll give that a shot.
Thanks!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|