Home:ALL Converter>Getting data from Ajax POST request in PHP?

Getting data from Ajax POST request in PHP?

Ask Time:2011-11-03T03:33:54         Author:bsand

Json Formatter

I'm trying to send a POST request with Ajax, but I'm having trouble getting the values sent in PHP. Here's my JavaScript code:

$.ajax({
    url: "updatedata.php",
    type: 'post',
    data: JSON.stringify(jsonData),
    contentType: 'application/json',
    dataType: 'json',
    success: function(data, status, xhr)
    {
       //...
    }
});

And I want to access the data with PHP. Something like this?

$data = $_POST['data'];

My data:

{"UID":"00a3b1b0-03b4-11e1-be50-0800200c9a66","Firstname":"Bastian","Lastname":"Sander","UserPenaltys":{"Penalty1":"110","Penalty10":"200","Penalty11":"210","Penalty12":"220","Penalty13":"230","Penalty14":"240","Penalty15":"250","Penalty16":"260","Penalty2":"120","Penalty3":"130","Penalty4":"140","Penalty5":"150","Penalty6":"160","Penalty7":"170","Penalty8":"180","Penalty9":"190"},"PenaltyCounter":16}

I tried this:

$.post("updatedata.php", JSON.stringify(UserData), function (data) {
}, "json");

But $_POST['Firstname'] is empty...

Author:bsand,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7986285/getting-data-from-ajax-post-request-in-php
WWW :

Why not use $.post()? The format is:\n\n$.post(<URI string>, <postdata object>, <handler>, <datatype>);\n\n\nAnd then treat the data like any other form post to PHP (i.e. use the $_POST variable).",
2011-11-02T19:36:52
Naftali :

Number one: you do not need to use JSON.stringify\n\nNumber two: Access them like so:\n\n$uid = $_POST['UID']; //...etc",
2011-11-02T19:35:51
yy