When we are Working in Ajax, Some times we have to send data to server json format or
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <script> function add(){ var jsondata = {name: 'freeonline', email:'freeonlinetest@gmail.com'}; var data = JSON.stringify( jsondata ); $.ajax({ type: "POST", url: 'addsome.php', data: data, success: function(data){ console.log(data); } }); } </script> |
Case 1:
addsome.php
In this case we are using $_POST as Data Holder.
1 2 | print_r($_POST); It will not work. It will return empty array |
Out put:-
Case 2:
In this case php://input as data holder
addsome.php
1 2 | $data= file_get_contents("php://input"); print_r($data); |
Out put:-
What is the Reason and why $_POST not working
The reason is that php://input returns all the raw data after the HTTP-headers of the request, It does not required any content type
however, $_POST method required content type like
1) application/x-www-form-urlencoded
2) multipart/form-data