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

前端之家收集整理的这篇文章主要介绍了php – Chrome中的JSON无效,Firefox中没问题(太奇怪了!)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所有键都是双引号.
整个元素是一个对象.
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","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","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","create_date":"17-05-2011 14:30:32","album_cover":"\/x\/media\/pwjps1231986828.jpg","description":""},{"album_id":63,"album_name":"CS","create_date":"17-05-2011 15:24:01","album_cover":"\/x\/media\/apple-logo.jpg",{"album_id":64,"album_name":"It works","create_date":"17-05-2011 15:24:56","album_cover":"\/x\/media\/it_works.png","description":""}]}

Firefox(Firebug)的JSON输出

{"code":0,"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" />
您在PHP文件的开头有一个Unicode Byte Order Mark.因此,并且因为它在开始之前<?PHP,它会在您的JSON开头发送到客户端.这将使您的JSON无效,因为这些字符不应出现在JSON数据的开头.有些浏览器可以很好地应对它;其他浏览器,如Chrome,更麻烦和抱怨. 通过保存文件而不在编辑器中设置该选项来删除字节顺序标记(如何执行此操作取决于编辑器)将解决您的问题. (您可能还会发现header()和其他发送标头的PHP函数也无法在您的PHP文件中运行,为您提供输出已经启动的错误,因为BOM将在您的PHP之前发送开始被解释.)
原文链接:https://www.f2er.com/php/137880.html

猜你在找的PHP相关文章