All keys were double quoted.
The whole element was an object.
Firefox runs it well but Chrome reports "invalid JSON". Why?
This is full code.
///////////// PHP ////////////////
public function listAlbumAction()
{
$params = $this->_getAllParams();
$albums = $this->_album->getAlbumList($params['albumType'], $params['from'], $params['numberOfAlbums']);
echo json_encode(array("code" => 0, "data" => $albums));
}
///////////////////////////////// JQuery ///////////////////
function loadAlbums()
{
$.ajax({
type: 'GET',
url: '/about-photo/list-album',
data: {albumType: selectedAlbumType, from: currentPageIndex * numOfAlbumsPerPage, numberOfAlbums: numOfAlbumsPerPage},
success: function(json) {
var obj;
var data;
try {
obj = $.parseJSON(json);
data = obj.data;
} catch(e) {
alert(e);
}
if(obj.code == 0) {
// get number of albums
var num = data.length;
// remove old list content
$('#albumListContent').remove();
var albumListHTML = '';
albumListHTML += '<div id="albumListContent">';
for(var i = 0; i < num; ++i) {
albumListHTML += '<div id="w' + data[i].album_id + '" class="imgWrapper">';
albumListHTML += '<img id="a' + data[i].album_id +
'" class="albumImg" width="150px" src="' +
data[i].album_cover + '" alt="not found" title="' +
data[i].album_name + '"/>';
albumListHTML += '<div class="albumTitle">' + data[i].album_name + '</div>';
albumListHTML += '</div>';
}
albumListHTML += '</div>';
$('#albumListContentWrapper').html(albumListHTML);
addAlbumHandler();
addPhotoEffects('.albumImg');
addImgErrorHandler('.albumImg');
}
}
});
}
Edit: JSON output from Chrome (FirebugLite):
{"code":0,"data":[{"album_id":42,"album_name":"Best album","album_type":"photo","create_date":"09-05-2011 5:48:40","album_cover":"/x/media/6.jpg","description":"Something here"},{"album_id":56,"album_name":"Test album","album_type":"photo","create_date":"09-05-2011 19:27:50","album_cover":"/x/media/44227440_2f1f369517.jpg","description":"apples"},{"album_id":59,"album_name":"Album for something","album_type":"photo","create_date":"10-05-2011 16:19:03","album_cover":"/x/media/apple-howto.jpg","description":"zzz"},{"album_id":62,"album_name":"Vietnam - Thailand - AFF Suzuki cup 2007","album_type":"photo","create_date":"17-05-2011 14:30:32","album_cover":"/x/media/pwjps1231986828.jpg","description":""},{"album_id":63,"album_name":"CS","album_type":"photo","create_date":"17-05-2011 15:24:01","album_cover":"/x/media/apple-logo.jpg","description":""},{"album_id":64,"album_name":"It works","album_type":"photo","create_date":"17-05-2011 15:24:56","album_cover":"/x/media/it_works.png","description":""}]}
JSON output from Firefox (Firebug):
{"code":0,"data":[{"album_id":42,"album_name":"Best album","album_type":"photo","create_date":"09-05-2011 5:48:40","album_cover":"/x/media/6.jpg","description":"Something here"},{"album_id":56,"album_name":"Test album","album_type":"photo","create_date":"09-05-2011 19:27:50","album_cover":"/x/media/44227440_2f1f369517.jpg","description":"apples"},{"album_id":59,"album_name":"Album for something","album_type":"photo","create_date":"10-05-2011 16:19:03","album_cover":"/x/media/apple-howto.jpg","description":"zzz"},{"album_id":62,"album_name":"Vietnam - Thailand - AFF Suzuki cup 2007","album_type":"photo","create_date":"17-05-2011 14:30:32","album_cover":"/x/media/pwjps1231986828.jpg","description":""},{"album_id":63,"album_name":"CS","album_type":"photo","create_date":"17-05-2011 15:24:01","album_cover":"/x/media/apple-logo.jpg","description":""},{"album_id":64,"album_name":"It works","album_type":"photo","create_date":"17-05-2011 15:24:56","album_cover":"/x/media/it_works.png","description":""}]}
I checked it with http://jsonlint.com/ and it says "Valid JSON"
Edit:
Source viewed from Chrome:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
See Question&Answers more detail:
os