http.responsetext

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

http.responsetext

Post by GeXus »

Is it posible to put the results of this into specificed variables? How would I do that? As apposed to just the entire response.. but rather grab certain variables.... for instance i am returning 2 values, which I need as seperate values
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Let say you are send these 2 values separated by a comma :

Code: Select all

results = http.responseText.split(",");
var a = results[0];
var b = results[1];
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yes, with AJAX you're basically dealing with strings so you need to send grouped data back in some easily parseable form you can split(). anjanesh uses a comma here which I try to avoid since it may be present in yor string. Instead, try something like a non-printable character to use as a delimiter. I had tried the NULL byte ( \0 ) previously which works.... GAHHH except in Opera which doesn't seem to be aware of the concept of a byte with a value of zero :( Instead, ascii delete works:

Code: Select all

echo "Value1\x7fValue 2";
exit();

Code: Select all

var parts = http.responseText.split('\x7f');

document.write(parts[0]); //Value1
document.wrtie(parts[1]); //Value2
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Better still...use XML - the X of AJAX.

Code: Select all

<value num="1"><?php echo "Value 1"; ?></value>
<value num="2"><?php echo "Value 2"; ?></value>
Its just that parsing xml properly in JavaScript is really a headache.
Post Reply