I am trying a simple test to learn about the JQuery Ajax POST method and I have setup a little example on my server. However I can't it to work.
My JavaScript is as follows:
JavaScript:
var json = {"Num String":"2","Num":3,"Num":11,"Num":2,"Num":"?"};
$.ajax({
type: "POST",
url: "sampleJSONPost.php",
data: json, //Data to POST to the server
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (jqXHR, status, err) {
console.log("Error " + err + " " + status + " " + JSON.stringify(jqXHR)); //Log the Error
},
success: function (data, status, jqXHR) {
console.log(JSON.stringify(data)); //Log the Data returned
$("span").text(data);
}
});
};
And here is my PHP:
PHP:
<?php
$data = $_POST["data"];
echo json_decode($data);
?>
However this always gives me the following error:
Error SyntaxError: Unexpected end of input parsererror {"readyState":4,"responseText":"","status":200,"statusText":"OK"}
So to me it looks like my $data variable in PHP is not getting the JSON I am sending with the POST, would this be correct? If so can anyone help me find what is wrong? I have already tried lots of other solutions from SO but without success so I must be missing something simple?