getJSON doesn't work
objecten.js
var data = [ {
image: '开发者_如何学Cgallery/objecten/bear.jpg',
thumb: 'gallery/objecten/bear.jpg',
title: 'my first image',
description: 'Lorem ipsum caption',
link: 'http://example.com'
}
];
In my main js file I am doing the following:
$.getJSON("gallery/objecten/objecten.js",function(data) {
$.each(data, function(key, value) {
alert("hello?");
});
});
The alert doesn't work, so what's wrong?
Your json is malformed. Despite of the format, you should not have a var
declaration. It should be:
objecten.js
[{
"image":"gallery/objecten/bear.jpg",
"thumb": "gallery/objecten/bear.jpg",
"title": "my first image",
"description": "Lorem ipsum caption",
"link": "http://domain.com"
}]
Note that strings and property names in json must be enclosed with "
To see more about the json "format" and its encoding rules see http://json.org
Hope this helps. cheers
object.js does not contain json
[{
"image": "gallery/objecten/bear.jpg",
"thumb": "gallery/objecten/bear.jpg",
"title": "my first image",
"description": "Lorem ipsum caption",
"link": "http://domain.com"
}]
Edited - I removed the parentheses that wrapped it, not sure why I put them in I guess I was looking at the example on http://docs.jquery.com/GetJSON which points to a flickr json page that uses parentheses. Works now.
精彩评论