一尘不染

Chrome中的JSON无效,Firefox中没有问题(太奇怪了!)

json

所有键都用双引号引起来。整个元素都是一个对象。 Firefox运行良好,但Chrome报告“无效的JSON”。 为什么?

这是完整的代码。

///////////// 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');
            }
        }
    });
}

编辑 :Chrome(FirebugLite)的JSON输出:

    {"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":""}]}

Firefox的JSON输出(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":""}]}

我使用http://jsonlint.com/进行了检查,并显示“有效JSON”

编辑

从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" />

阅读 217

收藏
2020-07-27

共1个答案

一尘不染

您的PHP文件开头有一个Unicode
字节顺序标记。正因为如此,并且因为它在开始之前<?php,所以它会在JSON的开头发送给客户端。这将使您的JSON无效,因为这些字符不应出现在JSON数据的开头。一些浏览器可以很好地处理它;其他浏览器(例如Chrome)则比较麻烦和抱怨。

通过在编辑器中未设置该选项的情况下保存文件来删除字节顺序标记(此操作取决于编辑器)将解决您的问题。

(您可能还会发现,header()发送标头的其他PHP函数也无法在您的PHP文件中使用,这同样会给您带来输出已经开始的错误,这再次是因为在解释您的PHP开始之前已经发送了物料清单)

2020-07-27