I just created a jQuery ajax function to extract some json-encoded data from PHP , here is my code:
file name: bank.php
$('form').on('submit', function(){ var datatobesent = $(this).serialize(); $.ajax({ data: datatobesent, url:'data.php', type:'GET' }) .done(function(data){ console.log(typeof(data)); }); return false; })
and in data.php I wrote
if(isset($_GET)){ $data = $_GET; echo json_encode($data); header("Content-type:application/json"); }
the question arises: when I delete the header("Content-type:application/json"); line header("Content-type:application/json"); in data.php , console.log says that the data type returned by ajax is string .
And when I added dataType : json,, inside ajax in bank.php , the type changes to object
So what is the header("Content-type:application/json"); function header("Content-type:application/json"); actually?
source share