php - json return null value after POST -
a.php
$(document).ready(function() { $("#submit_form").on("click",function(){ var json_hist = <?php echo $json_history; ?>; $.ajax({ type: "post", url: "b.php", data: "hist_json="+json.stringify(json_hist), //contenttype: "application/json; charset=utf-8", datatype: "json", success: function(data){alert(data);}, failure: function(errmsg) { alert(errmsg); } }); }); })
b.php
$obj=json_decode($_post["hist_json"]); var_dump($_post);
if comment contenttype: "application/json; charset=utf-8"
everything's works fine if uncomment this. var dump return null.
when set contenttype in ajax, setting contenttype request not response.
it fails json contenttype because data you're sending key/value formatted data (which missing encoding) , data doesn't match contenttype. json contenttype header when you're sending raw json no identifiers, in case have identifier hist_json=
.
i suggest changing to:
data: { hist_json : json.stringify(json_hist) },
using object hits_json
key mean jquery safely url encode json , allow php pick $_post['hits_json']
.
if want use json contenttype have change ajax to:
data: { json.stringify(json_hist) }, // <-- no identifier
and php:
$obj = json_decode($http_raw_post_data); var_dump($obj);
Comments
Post a Comment