简体   繁体   English

在Mamp系统上执行Ajax请求后无法显示json结果

[英]Can't display json result after ajax request on Mamp System

I installed Mamp to work locally on my website. 我安装了Mamp以便在我的网站上本地工作。 But something strange happened. 但是有些奇怪的事情发生了。 I have normal return on json (the firebug console display it) but the console log display "undefined" (!) 我在json上有正常的返回值(firebug控制台显示了它),但控制台日志显示了“ undefined”(!)

So firebug display my ajax request and json return : 所以萤火虫显示我的ajax请求和json返回:

POST http://local/test.php 200 OK 7ms
{"testjson":"ok"}

But console log display : undefined An idea ? 但是控制台日志显示:undefined一个主意?

I checked and json 1.2 is right enabled on Mamp. 我检查并在Mamp上正确启用了json 1.2。

test.html : 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 : test.php:

if($_POST['action']=="display")
{
   $response['testjson'] = "ok";
   header('Content-type: application/json');
   echo json_encode($reponse);
   exit;
}

Please remove the below line and try: 请删除以下行并尝试:

header('Content-type: application/json');

Or try one of these following variants: 或尝试以下变体之一:

$.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);
  }  
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM