I'm trying to POST some JSON data to be processed by PHP. When it arrives on the server-side, the data is escaped and I can't reliably unescape it.
var jsondata = '{"name":"Foo","email":"name@address.com"},{"name":"Bar","email":"name@address.com"}';
$.ajax({
type: "POST",
url: "save.php",
dataType: "json",
processData: false,
data: { json: '['+jsondata+']'},
});
$_POST['json']
then contains:
[{\"name\":\"Foo\",\"email\":\"name@address.com\"},{\"name\":\"Foo\",\"email\":\"name@address.com\"}]
How can I send unencoded JSON post-data? I thought processData: false
would cover that.
To illustrate the error server-side:
<?php
$_POST['json-bad'] = '[{\"name\":\"Foo\",\"email\":\"name@address.com\"},{\"name\":\"Foo\",\"email\":\"name@address.com\"}]';
$data_bad = json_decode($_POST['json-bad']);
var_dump($data_bad); // returns "NULL"
$_POST['json-good'] = '[{"name":"Foo","email":"name@address.com"},{"name":"Bar","email":"name@address.com"}]';
$data_good = json_decode($_POST['json-good']);
var_dump($data_good); // returns array as expected
?>