我安装Mamp在我的网站上本地工作。 但有些奇怪的事发生了 我在json上有正常的返回(firebug控制台显示它)但控制台日志显示“undefined”(!)
所以firebug显示我的ajax请求并且json返回:
POST http://local/test.php 200 OK 7ms
{"testjson":"ok"}
但是控制台日志显示:未定义 一个想法?
我检查过并且在Mamp上启用了json 1.2。
test.html:
<script type='text/javascript'>
$(document).ready(function(){
$.ajax({
type: "POST",
url: "mod/test.php",
data: "action=display",
success: function(response)
{
console.log(response['testjson']);
}
});
});
</script>
test.php:
if($_POST['action']=="display")
{
$response['testjson'] = "ok";
header('Content-type: application/json');
echo json_encode($reponse);
exit;
}
答案 0 :(得分:1)
请删除以下行并尝试:
header('Content-type: application/json');
或尝试以下以下变体之一:
$.ajax({
type: "POST",
url: "mod/test.php",
data: "action=display",
success: function(response) {
console.log(response.testjson);
}
});
$.ajax({
type: "POST",
url: "mod/test.php",
data: "action=display",
success: function(response) {
response = JSON.parse(response);
console.log(response['testjson']);
}
});
$.ajax({
type: "POST",
url: "mod/test.php",
data: "action=display",
success: function(response) {
response = JSON.parse(response);
console.log(response.testjson);
}
});