JavaScript Code:
$.ajax({
type: "POST",
url: "postTestingResult.php",
data: {data: JSON.stringify(sendData)},
dataType: "json",
success: ajaxSuccess,
error: ajaxError
});
PHP Code
$data = json_decode($_POST['data'], TRUE);
When I POST a complex data structure to the server, the outermost array is becoming a string. For example, the JavaScript object could be
var data = {"apps": [[1,2,3], [4,5,6]]}
Using JSON.stringify(data) this becomes
"{"apps": "[[1,2,3], [4,5,6]]"}" //As seen via console.log(data) in Chrome console
But after doing the json_decode($_POST['data'], TRUE) it becomes
array('apps' => '[[1,2,3], [4,5,6]]') //As seen via var_export($data, TRUE)
What's going on here? Why is the the array being converted to a string? To see the full JSON object and the full PHP object check out this pastebin with the two.
Any help is greatly appreciated, thank you.
UPDATE: Answer found
I found the main culprit. I am also using Prototype.js and it was adding a toJSON method to the Object prototypes. Check out this SO question for details.
See Question&Answers more detail:
os