简体   繁体   中英

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. 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? By default Chrome does not allow this. Try it in Firefox, if that works that was the issue. This will not happen if your HTML file is on a server in the web.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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