我刚刚创建了一个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");
}
问题是,当我删除 data.php
文件中的 header("Content-type:application/json");
行时,console.log
显示 ajax
返回的数据类型是 string
。
而当我在 bank.php
文件的 ajax
函数中添加 dataType:json
时,返回的类型变为 object
。
那么,header("Content-type:application/json");
的作用是什么?