Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > AJAX Question

AJAX Question
Thread Tools
nonhuman
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status: Offline
Reply With Quote
Oct 8, 2006, 05:01 AM
 
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.
     
Chris O'Brien
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status: Offline
Reply With Quote
Oct 12, 2006, 05:54 PM
 
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
     
registered_user
Dedicated MacNNer
Join Date: Nov 2001
Location: Are Eye
Status: Offline
Reply With Quote
Oct 12, 2006, 09:23 PM
 
sounds like you'll have to resort to some sort of text processing.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Oct 12, 2006, 09:28 PM
 
Is the mime type set to text/xml?
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
nonhuman  (op)
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status: Offline
Reply With Quote
Oct 27, 2006, 05:37 PM
 
Originally Posted by Chris O'Brien View Post
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.
     
nonhuman  (op)
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status: Offline
Reply With Quote
Oct 27, 2006, 05:41 PM
 
Originally Posted by Chuckit View Post
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...)
     
nonhuman  (op)
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status: Offline
Reply With Quote
Nov 15, 2006, 07:07 PM
 
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?
     
himself
Mac Elite
Join Date: Jan 2002
Location: Live at the BBQ
Status: Offline
Reply With Quote
Nov 18, 2006, 07:22 PM
 
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
     
nonhuman  (op)
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status: Offline
Reply With Quote
Nov 19, 2006, 10:44 AM
 
Originally Posted by himself View Post
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?
     
bri-guy
Junior Member
Join Date: Jul 2001
Location: Chicago, IL USA
Status: Offline
Reply With Quote
Dec 14, 2006, 09:45 PM
 
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).
     
nonhuman  (op)
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status: Offline
Reply With Quote
Dec 15, 2006, 12:21 PM
 
Ah, ok. I'll give that a shot.

Thanks!
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 10:44 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,