3

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.

5 Answers 5

6

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

Sign up to request clarification or add additional context in comments.

8 Comments

When using "myPostData", I get "Undefined index: myPostData". PHP gets an empty array.
@nush odd since that works for me with .NET web services.. I added another approach to my answer, please try it.
Here's an image: 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 in your PHP can you please var_dump($_POST) and show me the result?
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.
|
2

I found that just delete this line out

contentType: "application/json; charset=utf-8",

then you can manipulate the data in string

Comments

1

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

1 Comment

Even if using Fosco's suggestions (using "myPostData"), var_dump shows an empty array.
1

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.

6 Comments

Using this, I get an empty array.
Remove the contentType line, as I have just done in my edit.
Same thing. I think I'm gonna dig deeper than just syntax. I think it's something else.
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. :)
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.
|
0

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

1 Comment

No. dataType is the expected response format, not the format of the request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.