用$ .ajax加载json内容的问题

时间:2019-04-16 16:39:16

标签: javascript jquery json ajax

我正在使用ajax和jQuery加载json内容,但是当我打印它时,我什么也没得到。

index.php

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script>
        (function($){
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: 'content.json',
            success: function(data) {
                response = $.parseJSON(data);
                console.log(data);
                console.log(response);
            }
        });
    })(jQuery);
    </script>
</body>
</html>

content.json:

[
  {
    "name": "Meowsy",
    "species" : "cat",
    "foods": {
      "likes": ["tuna", "catnip"],
      "dislikes": ["ham", "zucchini"]
    }
  },
  {
    "name": "Barky",
    "species" : "dog",
    "foods": {
      "likes": ["bones", "carrots"],
      "dislikes": ["tuna"]
    }
  },
  {
    "name": "Purrpaws",
    "species" : "cat",
    "foods": {
      "likes": ["mice"],
      "dislikes": ["cookies"]
    }
  }
]

为什么我的控制台什么都没有?

如果我要打印例如sony / id(1)/ game ...,请问正确的代码是什么?

0 个答案:

没有答案