在 Mamp 系统上 ajax 请求后无法显示 json 结果
Can't display json result after ajax request on Mamp System
我安装了 Mamp 以在我的网站上本地工作。
但是奇怪的事情发生了。
我在 json 上有正常的 return(firebug 控制台显示它)但是控制台日志显示 "undefined" (!)
所以 firebug 显示我的 ajax 请求和 json return :
POST http://local/test.php 200 OK 7ms
{"testjson":"ok"}
但是控制台日志显示:未定义
一个想法?
我检查了一下 json 1.2 在 Mamp 上正确启用。
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;
}
请删除以下行并尝试:
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);
}
});
我安装了 Mamp 以在我的网站上本地工作。 但是奇怪的事情发生了。 我在 json 上有正常的 return(firebug 控制台显示它)但是控制台日志显示 "undefined" (!)
所以 firebug 显示我的 ajax 请求和 json return :
POST http://local/test.php 200 OK 7ms
{"testjson":"ok"}
但是控制台日志显示:未定义 一个想法?
我检查了一下 json 1.2 在 Mamp 上正确启用。
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;
}
请删除以下行并尝试:
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);
}
});