javascript - Cannot get data in PHP from JQuery Ajax post with JSON -
i trying simple test learn jquery ajax post method , have setup little example on server. can't work.
my javascript follows:
javascript:
var json = {"num string":"2","num":3,"num":11,"num":2,"num":"?"}; $.ajax({ type: "post", url: "samplejsonpost.php", data: json, //data post server contenttype: "application/json; charset=utf-8", datatype: "json", error: function (jqxhr, status, err) { console.log("error " + err + " " + status + " " + json.stringify(jqxhr)); //log error }, success: function (data, status, jqxhr) { console.log(json.stringify(data)); //log data returned $("span").text(data); } }); };
and here php:
php:
<?php $data = $_post["data"]; echo json_decode($data); ?>
however gives me following error:
error syntaxerror: unexpected end of input parsererror {"readystate":4,"responsetext":"","status":200,"statustext":"ok"}
so me looks $data variable in php not getting json sending post, correct? if can me find wrong? have tried lots of other solutions without success must missing simple?
change into
var json = {"data":{"num string":"2","num":3,"num":11,"num":2,"num":"?"}};
Comments
Post a Comment