{
"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
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
parameters : '?method=saveData&data='+data.toJSON();
--
This is just so totally awesome... god this is the best day EVER!