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

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

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

Thanks for any suggestions, Andrew

+4
source share
4 answers

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

+5
source

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

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

+5
source

In the JavaScript Javascript side (with Prototye):

var myJSON= Object.toJSON(youArray); 

Aside que php:

 $myjson = $_POST['myjson']; $arrayJSON= json_decode(stripslashes($myjson), true); 
+2
source

Checking http://www.openjs.com/scripts/data/ued_url_encoded_data/ to correctly encode the attached data, since Object.toQueryString () does not accept attached data ...

0
source

All Articles