Multi-dimensional JSON passed in an Ajax request?

22 views
Skip to first unread message

Ian R

unread,
Dec 17, 2009, 5:14:14 PM12/17/09
to Prototype & script.aculo.us

So ok, I have this JSON string I'm trying to pass as a post. From my
object's toJSON() method, I get this string:

{
"anonymous_element_1": {
"class": "required",
"msg": ""
},
"anonymous_element_2": {
"class": "required",
"msg": ""
},
"public_phone": {
"class": "phone",
"value": "706-201-1149",
"msg": ""
},
"private_phone": {
"class": "phone",
"value": "315-487-9176",
"msg": ""
}
}

Which is valid JSON according to http://www.jsonlint.com/ ...

However, when I try to pass this to a PHP script:

new Ajax.Updater(this.msg, '/inc/Calendar/Validate.php',
{
'parameters': this.toJSON()
}
);

I get nothing passed.

However, I've noticed that if I take the multidimensional aspect out
of it and just use the format {'key':'value,'key':'value'}, it passes
just fine. Also, if I specifically set the "method" to "get", I this
passed in $_GET

Array
(
[{"anonymous_element_1":_
{"class":_"required",_"msg":_""},_"anonymous_element_2":_
{"class":_"required",_"msg":_""},_"public_phone":_
{"class":_"phone",_"value":_"706-201-1149",_"msg":_""},_"private_phone":_
{"class":_"phone",_"value":_"315-487-9176",_"msg":_""}}] =>
)

Basically a crazy URL-encoded version of my JSON, as the key in a
valueless array. Hm.

What am I doing wrong here? I just don't get it.

Thanks in advance!

Ian

T.J. Crowder

unread,
Dec 17, 2009, 6:48:39 PM12/17/09
to Prototype & script.aculo.us
Hi,

Ajax.Updater (and the rest) don't claim to post object graphs to the
server, and in fact they don't. They only pass name-value pairs,
because they're doing normal HTTP GETs or POSTs, which are based on
name-value pairs. HTTP doesn't know anything about JSON.

If you want to pass an object graph, you could pass a single parameter
with your JSON-formatted data as the value:

new Ajax.Updater(this.msg, '/inc/Calendar/Validate.php',
{
'parameters': {json: this.toJSON()}
}
);

...and then decode the JSON-formatted data (the value of the 'json'
parameter) on the server. There are JSON libraries for most server-
side languages (and if there isn't one that suits your environment,
well, the point of JSON is that it is easily parsed -- see json.org
for details).

HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com

Matt Foster

unread,
Dec 18, 2009, 10:33:31 AM12/18/09
to Prototype & script.aculo.us
I'd just put it in a variable.

parameters : '?method=saveData&data='+data.toJSON();


--

http://positionabsolute.net

Ian R

unread,
Dec 18, 2009, 11:47:19 AM12/18/09
to Prototype & script.aculo.us

Ahhh. Thank you so much, sir! This solution works perfectly for my
needs. And yes, it does make sense, I just didn't realize.

This is just so totally awesome... god this is the best day EVER!

Ian R

unread,
Dec 18, 2009, 11:53:08 AM12/18/09
to Prototype & script.aculo.us

Oh, by "sir" I mean T.J. Crowder (I hadn't yet seen the second
reply). I don't mean to crud up the list-serv, and thank you Matt
Foster, but I much prefer the first solution... just wanted to
clarify :)
Reply all
Reply to author
Forward
0 new messages