我刚刚创建了一个
JQuery ajax函数来从
PHP中检索一些json编码的数据,这是我的代码:
文件名: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;
})
在data.php我写道
if(isset($_GET)){
$data = $_GET;
echo json_encode($data);
header("Content-type:application/json");
}
问题是,当我删除标题行时(“Content-type:application / json”);在data.php中,console.log告诉ajax返回的数据类型是string.
当我在bank.php中的ajax函数中添加dataType:json“时,类型会变为object
那么header的功能是什么(“Content-type:application / json”);其实?