首页 > 解决方案 > 使用 ajax 在 javascript 中使用 json 的数据

问题描述

错误

我在我的 html 文档中使用 json 文件的数据时遇到问题。

在顶部,您可以看到我得到的错误。

我将此文件创建为 json 文件 New.json :

[
    { "firstname":"Thomas", "lastname":"Anderson" },
    { "firstname":"Agent", "lastname":"Smith" },
    { "firstname":"Agent", "lastname":"Brown" }
]

我尝试使用此代码从我的 json 文件中获取数据

    <script type="application/javascript">
            $(document).ready(function() {
                $("div").click(function () {
                    $.ajax({
                        type: 'GET',
                        url: './New.json',
                        dataType: 'json',
                        success: function(data){
                            window.alert(data);
                        },
                    });
                });
            });
    </script>

谢谢你的帮助 :)

标签: javascripthtmljqueryjsonajax

解决方案


您的 URL 可能需要是: url: './New.json',


推荐阅读