Chrome中无效的JSON,Firefox中没有问题(太奇怪了!) [英] Invalid JSON in Chrome, no problem in Firefox (so strange!)

查看:96
本文介绍了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 /// ////////////////
函数loadAlbums()
{
$ .ajax({
类型:'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){
//获取专辑数量
var num = data.length;

//删除旧列表内容
$('#albumListContent')。remove();

var albumListHTML ='';
albumLis tHTML + ='< div id =albumListContent>'; (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 =albumImgwidth =150pxsrc ='+
data [i] .album_cover +'alt =not foundtitle ='+
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');
}
}
}); (FirebugLite)输出的JSON输出结果显示,Chrome浏览器(JRE) :

  {code:0,data:[{album_id:42,album_name:Best相册,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, 说明: },{ album_id:63, ALBUM_NAME: CS, album_type:照片,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(Firebug)的JSON输出:

  {code:0,data:[{album_id:42,album_name:最佳专辑,album_type:照片,create_date:09-05-2011 5:48:40,album_cover:\ / x \ / media\ / 6.jpg,description:这里有些东西} ,album_id:56,album_name:Test album,album_type:photo,create_date:09-05-2011 19:27:50,album_cover:\ /描述:苹果},{album_id:59,album_name:某物的专辑,album_type:照片,创建日期 :10-05-2011 16:19:03,album_cover:\ / x \ / media\ / apple-howto.jpg,description:zzz},{album_id :62,album_name:越南 - 泰国 -  AFF铃木杯2007,album_键入:照片,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 // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> 
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>


解决方案

您有一个Unicode 字节顺序标记在PHP文件的开头。正因为如此,并且因为它位于开头<?php 之前,所以它会在JSON开始时发送到客户端。这会使你的JSON无效,因为这些字符不应该出现在JSON数据的开头。有些浏览器可以很好地处理它;其他浏览器,如Chrome浏览器,更加恼人和抱怨。

删除字节顺序标记通过保存没有在您的编辑器中设置的选项来保存文件(如何做到这一点是编辑器你可能会发现 header()和其他PHP发送头文件的函数在你的PHP文件中也不起作用,给你输出已经开始的错误,这也是因为在开始解释PHP之前,BOM已经被发送了。)


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

解决方案

You have a Unicode Byte Order Mark at the beginning of your PHP file. Because of this, and because it's before the opening <?php, it gets sent to the client at the beginning of your JSON. This will make your JSON invalid, as those characters shouldn't appear at the beginning of the JSON data. Some browsers cope with it fine; other browsers, such as Chrome, are fussier and complain.

Removing the Byte Order Mark by saving the file without that option set in your editor (how to do this is editor-dependent) will solve your problem.

(You'd probably also find that header() and other PHP functions that send headers wouldn't work in your PHP file, either, giving you the error that output has already started, again because the BOM would have been sent before your PHP started being interpreted.)

这篇关于Chrome中无效的JSON,Firefox中没有问题(太奇怪了!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆