Reputation: 3392
I won't tell you I've searched and tried dozens of syntaxes from the internets. You couldn't tell if I'm lying or not. So...
var jsonData = {
address: 'address',
address1: 'address1',
address2: 'address2'
};
var out = JSON.stringify(jsonData);
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "joaca2.php",
data: out,
dataType: "html",
success: function (response) {
alert(response);
}
});
$x = json_decode($_POST, true);
// don't worry: it doesn't get to this line below
printf("<pre>%s</pre>", print_r($x, 1));
I've tried to keep it as simple as possible, maybe some time this year I'll learn about proper JSON.
The last image is what I get when the PHP part has this:
var_dump(file_get_contents('php://input'));
Don't start with "Isn't it obvious?!". It is. I know what that error says. I just don't know how to get around it. How am I to grab that post? I've seen $x = json_decode($_POST[]), but that doesn't work either. I've tested the stringified json with JSONlint and it validated. I've tried different types of arrays, objects, array properties, .AJAX, .post(), .get(). I'm out of known options. I've seen all kinds of suggestions and I've pretty much tried them. I know I'm missing something and I'll probably explode or kill my cat when I'll find it.
Thanks, as always
I modified with data: 'kkt=' + out in the code. Now, using this:
$x = json_decode($_POST['kkt'], true);
echo $x['myPostData']['address1'];
...I can get the value. The problem is I don't know how this really works. I know it's a key, though.
Upvotes: 3
Views: 7865
Reputation: 49
I found that just delete this line out
contentType: "application/json; charset=utf-8",
then you can manipulate the data in string
Upvotes: 2
Reputation: 237845
Is there a reason you're using JSON.stringify
if all you want to do is convert to a PHP array the other end? Why not do
$.ajax({
type: "POST",
url: "joaca2.php",
data: jsonData, //or whatever name
dataType: "html",
success: function (response) {
alert(response);
}
});
and then in the PHP
var_dump($_POST);
This will pass the data to your script as key-value pairs, rather than a bloated JSON representation. HTTP encoding (done natively by jQuery) should be enough for you here.
Upvotes: 1
Reputation: 38516
$_POST is an array of all posted elements... You're only passing one element, but you're not assigning a name to it.
Try using
$x = json_decode($_POST[0]);
Though, what I would do is:
var out = JSON.stringify({'myPostData' : JSON.stringify(jsonData) });
and then:
$x = json_decode($_POST['myPostData']);
How about altering the original jsonData to include a main branch:
var jsonData = { myPostData: {
address: 'address',
address1: 'address1',
address2: 'address2'
} };
and then returning the original stringify function call.
Upvotes: 6
Reputation: 24182
You are trying to run json_decode on the whole POST, which is an array. You should understand which key holds your ajax string, and call it like this:
json_decode($_POST['key'])
to understand how is your key called, just dump the $_POST with var_dump...
Upvotes: 1