php - Jquery getJSON php 数组到 javascript

标签 php javascript jquery getjson

我正在使用 php 数组测试 getJSON,但它不起作用,有人可以帮我检查一下吗?我觉得问题出在 php 代码上,因为当我使用 getJSON url https://graph.facebook.com/zombies 测试 html 代码时, 这是工作。

在我的 array.php 中

<?php

 header("Content-type: text/javascript");

 $arr = array(

            "name" => "Tim",
            "age" => "28"     );

echo json_encode($arr);

?>

在我的 test.html 中:

  <html>
  <head>
    <script type='text/javascript' src='jquery.js'></script>
  </head>
  <body>

    <script type='text/javascript'>

    $(document).ready(function() {
         $.getJSON('array.php', function(data) {        
            if(data) {
            document.write(data.age);       
            }
            else {
            alert('error');
            }
        });
    }); 
    </script>

   </body>
   </html>

最佳答案

改变

header("内容类型:文本/javascript");

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

对于 JSONP

header('Content-Type: application/javascript');

关于php - Jquery getJSON php 数组到 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13539964/

相关文章:

javascript - 如何克隆元素及其关联的事件回调?

javascript - 谷歌在一页上绘制两个条形图

javascript - 为什么 Visualforce 中的 jquery 不起作用?

PHP Warning : mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, string in given in ..... with SELECT MAX

PHP mysqli_query 报语法错误

javascript - 为什么渲染时会出现错误?

javascript - 在 IE 10 中 javascript 数组不起作用

javascript - Bootstrap Popover 不适用于 Rails

php - 在 Symfony2 和 Twig 中为文件添加资源路径前缀

php - PHP 中的 CSS 转换器