Vasileios Tsakalis
Vasileios Tsakalis

Reputation: 1211

jQuery , json data call doesnt work

Here is a sample of my code:

events.json // This file is in an external page

[
    {'id':1,'name':'george'},
    {'id':2,'name':'john'},
    {'id':3,'name':'nick'}
]

The json call:

$.getJSON('events.json', function(data) {
    $(data).each(function(i,val) {
        alert(data[i].name);
    });
});

Upvotes: 0

Views: 48

Answers (1)

MrCode
MrCode

Reputation: 64526

Your JSON is invalid. Identifiers and values must use double quotes not single quotes. jQuery is not able to parse the JSON and so your callback will not be executed.

Avoid manually creating JSON. Whatever programming language you use to produce the JSON, always use the relevant library to build the JSON.

You can validate your JSON at JSONLint.

Upvotes: 2

Related Questions