【问题标题】:cant load json data from .json file无法从 .json 文件加载 json 数据
【发布时间】:2017-01-06 08:27:07
【问题描述】:

我正在尝试从文件中加载 json 对象.. 但它总是会出现错误消息

<html>

   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript"
         src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("#driver").click(function(event){
                $.ajax({
                    url: 'file.json',
                    type: 'get',
                    dataType: 'json',
                    error: function(data){
                        $('#stage').html("error");
                    },
                    success: function(data){
                        $('#stage').html("ay 7aga");
                        }
                    });
            });
         });
      </script>
   </head>

   <body>

      <p>Click on the button to load result.html file:</p>

      <div id = "stage">
         STAGE
      </div>

      <input type = "button" id = "driver" value = "Load Data" />

   </body>

</html>

还有我的file.json

{"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]}

这是文件位置

关于成功消息的任何建议? 它说Failed to load resource: the server responded with a status of 404 (Not Found)

请帮助我是初学者 任何建议请..提前谢谢

【问题讨论】:

  • 在我看来,您将 json 文件放置在错误的位置。我假设您拥有 index.html 的文件夹是您的网络根目录。所以这就是你的 json 文件需要的地方。假设是一个静态文件服务器。

标签: javascript java jquery json jakarta-ee


【解决方案1】:

请检查您的文件位置,它是否正确放置在您的机器中。

          $.ajax({
                    url: './file.json', //Check this file location.
                    type: 'get',
                    dataType: 'json',
                    error: function(data){
                        $('#stage').html("error");
                    },
                    success: function(data){
                        $('#stage').html("ay 7aga");
                        }
                    });
            });

如果位置正确,那么您使用的是什么服务器? 如果是 IIS,则在服务器中添加 MIME 类型为 JSON

【讨论】:

  • 我现在就截屏
【解决方案2】:

尝试使用 url:'../file.json' 即对 json 文件的上一级引用

【讨论】:

    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 2020-09-29
    • 2013-09-05
    相关资源
    最近更新 更多