I am trying to pass a JSON object to a PHP page, this is my jquery script.
reg_id = $('#registroid').val();
razonid = $("#selectRazonId").val();
$.ajax({
data: JSON.stringify({myData:{razonid:razonid,reg_id:reg_id}}),
type: "POST",
dataType: "json",
url: "inc/sitctp0013Procesa-2.php",
contentType: "application/json; charset=utf-8",
success: function(data){
alert(data);
}
});
And this is my PHP script
<?php
$myPostData = json_decode($_POST['myData']);
$firstname = $myPostData["razonid"];
$lastname = $myPostData["reg_id"];
if($myPostData){
echo "good";
} else{
echo "bad";
}
?>
but is only printing "bad", I tried doing a var_dump($_POST); but I'm getting a null result.
If I look at the firebug console I can see that the JSON object it is being sent
JSON.stringify, drop that part. It should just bedata: { myData: { ... } }. You also don't need thecontentTypeoption.