简体   繁体   English

无法使用jQuery从JSON数组正确打印数据

[英]Can't correctly print data from JSON array using jQuery

I've got this simple array of data in a json file but trying to extract the data is proving hard for a novice like me. 我在json文件中有这个简单的数据数组,但是对于像我这样的新手来说,尝试提取数据非常困难。 Here is the array that I have been given. 这是我得到的数组。

var clients = [
{
    "clientid": "456489",
    "client-name": "John Smith",
    "email": "a.smith@gmail.com"        
},
{
    "clientid": "654987",
    "client-name": "Mark Barnes",
    "email": "barnesdog@gmail.com"
},
{
    "clientid": "987981",
    "client-name": "Taylor Johnson",
    "email": "taylor.j@gmail.com"
},
]

My first test was this to see if I could get a success message or not. 我的第一个测试是查看是否可以收到成功消息。 It has not worked and i'm just not sure why. 它没有用,我只是不确定为什么。

$(document).ready(function() {
     $.getJSON("clients.json", function(data){
         $.each(data, function (index, value) {
             console.log(data);
         });
     });
});     

I know this is simple, but I have never done this before, thanks in advance. 我知道这很简单,但是我之前从未做过,谢谢。

Are you using Chrome and opening the HTML file that contains the code from file? 您是否正在使用Chrome浏览器并打开包含文件中代码的HTML文件? By default Chrome does not allow this. 默认情况下,Chrome不允许这样做。 Try it in Firefox, if that works that was the issue. 如果可以,那就在Firefox中尝试一下。 This will not happen if your HTML file is on a server in the web. 如果您的HTML文件位于网络中的服务器上,则不会发生这种情况。

从顶部的json和var clients =对象列表中删除尾随的,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM