Best way to pass JSON from Browser to PHP using Ajax.Request

Hi I have a JSON object that is a 2-dimentional array and I need to pass it to PHP using Ajax.Request (only way I know how). ...Right now I manually serialized my array using a js function...and get the data in this format: s[]=1&d[]=3&[]=4 etc. ....

my question is: Is there a way to pass the JSON object more directly/efficientely?..instead of serializing it myself?

Thanks for any suggestions, Andrew

Answers


You can also use Prototype's function toJSON() to convert an array into a JSON object. After passing it to server via Ajax call, simply use PHP's function json_decode() to decode the object.


Pass the object as a JSON-string to PHP, and in PHP use the builtin json_decode to get a PHP-object from the string.

In Javascript, use a "stringify" function on your object to get it as a string, library available for example here: https://github.com/douglascrockford/JSON-js/blob/master/json2.js


In que Javascript side (with Prototye):

var myJSON= Object.toJSON(youArray);

In que Php side:

$myjson = $_POST['myjson'];

$arrayJSON= json_decode(stripslashes($myjson), true);

Check http://www.openjs.com/scripts/data/ued_url_encoded_data/ to encode nested data directly correct, since Object.toQueryString() doesn't accept nested data...


Need Your Help

SCons - integrating custom builders for CUDA, CORBA etc

python c++ cuda scons corba

For a a heterogeneous (C, C++14, Fortran95, python3) project, I'm currently evaluating what advantages over CMake SCons will provide to us for the integration of platform compilers and compiler wra...

click on a element of a list html5 javascript

javascript html5

I'm trying to click on a element of a list that I create like this: