data visible in Request Payload section of networking however the data is not available in POST

Viewed 49

I am trying to pass json with ajax, the call is goin okay. I can see the data at the Request Payload section but not in post variable.

var p = {
            c: c,
            g: g,
            t: t
        };

    var myJSON= JSON.stringify(p);

  $.ajax({
            url: "addedit.php",
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json',
            data: myJSON,
            success: function () {
                alert("success");
            }
        });

PHP code :

$myjson= array();

$myjson= json_decode($_POST['myJSON']);

var_dump($myjson);die;

networking section

POST data

1 Answers

You have to pass the data as the object

data: {
    myJSON:              myJSON
},

Full ajax request

$.ajax({
    url: "addedit.php",
    type: 'POST',
    data: {
        myJSON:              myJSON
    },
    success: function () {
        alert("success");
    }
});

My Input data

var p = {
        c: 1001,
        g: 4,
        t: 100
    };

In your php file just do this

$myjson= json_decode($_POST['myJSON'],true);
var_dump($myjson);

The output of var_dump below

array(3) { ["c"]=> int(1001) ["g"]=> int(4) ["t"]=> int(100) }
Related