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;
My Answer:
In your PHP code, you are trying to access the JSON data sent in the POST request using $_POST['myJSON'], but in your AJAX call, you are sending the data without a key.
You should update your PHP code to access the JSON data without a key like this:
php
$myjson = json_decode(file_get_contents('php://input'), true);
var_dump($myjson);
die;
By using file_get_contents('php://input'), you can access the raw POST data sent in the request payload. This should allow you to access the JSON data sent from your AJAX call.
Rate this post
3 of 5 based on 5855 votesComments