I'm sending JSON to backend server, but I'm confuse about how I can process it correctly,
i'm reading around that echo (json_decode($_POST));
is not going to work but
echo (json_decode(file_get_contents("php://input")));
, but actually I tried to output the response on the client side alert(respon);
but nothing show
0_12_e2_contentType_JSON.html
<html>
<head>
<script type="text/javascript" src="jquery-2.0.3.min.js"></script>
</head>
<body>
<script >
$(function(){
$.ajax({
url: "fragment/0_12_e2_contentType_JSON.php",
type: "POST",
contentType:"application/json; charset=utf-8",
data: {
"name":"hans" ,
"id":10
},
success: function (respon)
{
alert(respon);
},
error:function(e){
alert('Ajaxnya error');
}
});
});
</script>
</body>
</html>
0_12_e2_contentType_JSON.php
<?php
echo (json_decode(file_get_contents("php://input")));
?>
My question is that is it already correct? but why does it output nothing? Thanks