javascript - 无法使用 jQuery 从本地 json 文件加载数据

标签 javascript jquery

<!DOCTYPE html>
<html>
  <head>
    <script data-require="jquery@*" data-semver="3.0.0" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script type="text/JavaScript">
         $select = $('#people');
         //request the JSON data and parse into the select element
         $.ajax({
             type:'GET',
             url: 'simple.json',
             dataType:'JSON',
             success:function(data){
                 //clear the current content of the select
                 $select.html('');
                 //iterate over the data and append a select option
                 $.each(data.person, function(key, val){
                     $select.append('<option >' + val.firstName+ '</option>');
                 })
             },
             error:function(){
                 //if there is an error append a 'none available' option
                 $select.html('<option >none available</option>');
             }
         });
         </script>
  </head>

  <body>
    <select id="people"></select>
  </body>

</html>

无法使用 jQuery ajax 读取本地 json 文件中的数据。

即使我正在使用 $.getjson() 尝试相同的代码。未返回输出。

这是我的笨蛋:https://plnkr.co/edit/Ys2xxOn1oUCeNL3wqcvJ?p=preview

最佳答案

data.person更改为data.employees,将代码包装在文档就绪语句中

https://plnkr.co/edit/PemDjZFAsFCcH16NfdQu?p=preview

关于javascript - 无法使用 jQuery 从本地 json 文件加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37802787/

相关文章:

javascript - 自动填写用户名和密码

javascript for 循环和函数

javascript - 设置 "selected"动态改变 html select 的值

jquery - 使用 jQuery 读取 RSS 提要?

javascript - 将 javascript 数组转换为 Float32Array 到缓冲区并返回

javascript - RxJS - 我需要退订吗

c# - 如何在扩展 Kendo Treeview 上加载子节点

javascript - 优先考虑 AJAX 调用

javascript - JQUERY 在多个无序列表中选择第 n 个列表元素

javascript - 如何使用 JQuery 根据屏幕尺寸实时更新 CSS?