1

JavaScript code:

var data = {"hiSO": "my very complex - nested objects/arrays - data object"};

var j = jQuery.noConflict();
j.ajax({
  type: "POST",
  url: "postTestingResult.php",
  contentType: "application/json; charset=utf-8",
  data: {"data": JSON.stringify(data)},
  dataType: "json",
  success: ajaxSuccess,
  error: ajaxError
});

PHP Code:

header('Content-Type: application/json');

if(!empty($_POST['data'])) {
    $data = json_decode($_POST['data']);

    //do things with data

    echo json_encode(array("success" => "thanks for the info!"));
} else {
    echo json_encode(array("error" => "'data' is not set or is NULL"));
}

No matter how I structure the data, $_POST['data'] always seems to be empty (specifically, Undefined). If you want a full copy of the data object I am using, check out this JSFiddle. Appreciate the help, thank you!

Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128

1 Answers1

6

You've told the server you're sending JSON (via contentType: "application/json; charset=utf-8"), but you're not sending JSON, you're sending URI-encoded data. This is because you've given jQuery an object for the data option (an object with one property, whose value is a JSON string).

Your PHP code expects URI-encoded data, so to fix it just remove the contentType option so jQuery uses its default.

If you really want to send JSON instead (which would require changing your PHP), change

data: {"data": JSON.stringify(data)},

to

data: JSON.stringify(data),

or

data: JSON.stringify({data: data}),
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks! I'm not sure I would have ever though of that! And I edited my question to include the var j = jQuery.noConflict(); so as to remove confusion for others. – Matthew Herbst Apr 02 '14 at 03:41
  • As a follow-up question, is there any reason why I should not send as JSON? Can i just sent it plain? I've always been under the assumption it had to be in some format, JSON and XML being the two most popular – Matthew Herbst Apr 02 '14 at 03:41
  • @MatthewHerbst: You have to send data in *some* format. The most popular format for *sending* to the server is URI-encoded (technically, `application/x-www-form-urlencoded; charset=UTF-8`), which is what you're using in your outer layer (your POST contains a single URI-encoded field, `data`; the value of that field is a JSON string). The popular formats for sending data *from* the server to the client are JSON and XML. For sending complex data to the server, JSON-over-URI-encoding (what you've done) seems just fine, and I've used it in the past. – T.J. Crowder Apr 02 '14 at 03:51
  • @T.J.Crowder Why `would require changing your PHP` if he send his `data` as `JSON`? I think his PHP code is OK for that case? – Felix Apr 02 '14 at 04:10
  • @Felix I'm a bit confused on what to do for the PHP side of it too. I like the `data: JSON.stringify({data: data}),` way of doing it because that puts all my data within the $_POST['data'] field. However, does PHP automatically decode the JSON? If not, how does PHP know to put data into `$_POST['data']` if it just gets posted a big JSON string? I guess this comes down to asking where exactly I do the `json_decode()` – Matthew Herbst Apr 02 '14 at 04:51
  • @Felix: He's using `$_POST` to access a POST variable called `data` and then calling `json_decode` on that variable's value. That works if he sends URI-encoded data because PHP decodes the URI-encoded data, and sees that it says to create a variable called `"data"` with a big string value. PHP doesn't know what the string contains; the OP's code does, though, and uses `json_decode`. If he sends the POST data using `Content-Type` `application/json`, then either PHP has to interpret the JSON (which I don't think it does), or he has to *not* use `json_decode` (because PHP interpreted the data). – T.J. Crowder Apr 02 '14 at 05:04
  • @MatthewHerbst: If you're sending the data using `data: {data: JSON.stringify(...)}` and *not* using `contentType: "application/json..."`, then your PHP code is using `json_decode` in the right place. – T.J. Crowder Apr 02 '14 at 05:07
  • Worked perfectly. Thanks so much - I really appreciate it! – Matthew Herbst Apr 02 '14 at 05:22
  • @T.J.Crowder I've run into a problem. For some reason, arrays are being interpreted as strings once json_decoded in PHP. For example, {"apps": [1,2,3]} gets encoded to JSON as "{"apps":"[1,2,3]"}" and then in PHP this for some reason becomes array('apps' => '[{"app_id": 0, ...}]') which obviously screws up trying to access any elements in 'apps' since it is now a string rather than an array. Any idea what's going on? [Check out this pastebin](http://pastebin.com/WwGnSctn) for the full JSON and PHP data objects. Thank you – Matthew Herbst Apr 04 '14 at 00:36
  • I've [posted a question](http://stackoverflow.com/questions/22851766/why-is-json-decodedata-true-converting-an-array-to-a-string) for this – Matthew Herbst Apr 04 '14 at 01:11