views:

67

answers:

4

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...

This is part of my html (the relevant part):

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);
    }
});

And this is the PHP part:

$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.


Here's what I get:

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 think I nailed it:

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.

+2  A: 

$_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.

Fosco
When using "myPostData", I get "Undefined index: myPostData". PHP gets an empty array.
nush
Annexed for the above comment: http://i52.tinypic.com/2ij5fdx.png
nush
@nush odd since that works for me with .NET web services.. I added another approach to my answer, please try it.
Fosco
Here's an image: http://i52.tinypic.com/dr2ie.png .No, it has to be something like the Winamp version I'm using or the shoes I'm wearing. What's your specs? I'm on PHP 5.3.1, FF 3.6.11, jQuery 1.4.3, Firebug latest (whatever that is). Do they count/interfere? Sorry, I forgot to mention: PHP dumps an empty array.
nush
@nush in your PHP can you please var_dump($_POST) and show me the result?
Fosco
The problem is that the POST doesn't get populated. From thereon I think I can manage. That var_dump is the first thing I check.
nush
@nush you have some other issue then, probably server/configuration related... can you try making a static HTML form that posts to the same page and see if $_POST gets populated?.
Fosco
@Fosco - it works when POSTing from a plain form. $_POST gets populated. Thanks for your time. I think I'm gonna watch for something else.
nush
OK, I think I've got it. I just modified with this: **data: 'kkt=' + out** at the corresponding line. Now i can echo **$x['myPostData']['address1']** from **$x = json_decode($_POST['kkt'], true)** and get that value.Care to share what happened?
nush
+1  A: 

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.

lonesomeday
Using this, I get an empty array.
nush
Remove the `contentType` line, as I have just done in my edit.
lonesomeday
Same thing. I think I'm gonna dig deeper than just syntax. I think it's something else.
nush
Have a look at http://pastebin.com/WKBsYsyf This code works for me.
lonesomeday
This one works for me, too, thanks. But I was especially on to get JSON. It works for everybody, but not for me and I wanted to know why. :)
nush
Don't use a sledgehammer to crack a nut! However, if you really want to use JSON, try doing `json_decode(file_get_contents('php://input'));` Note that `php://input` is *not* identical to `$_POST`.
lonesomeday
Yup, I get a full-fledged stdClass Object. http://i55.tinypic.com/2d1mplw.png and http://i53.tinypic.com/71ixi8.png . THAT'S what I'm aiming at with JSON. But this time is "application/x-www-form-urlencoded".I know what you're saying, but it's not a project, it's for my own knowledge.
nush
+1  A: 

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...

Palantir
Even if using Fosco's suggestions (using "myPostData"), var_dump shows an empty array.
nush
A: 

dataType: "html" should not that be dataType: "json"?

Yasen Zhelev
No. `dataType` is the expected response format, not the format of the request.
lonesomeday