JQuery不接受JSON有效

时间:2013-03-11 11:25:00

标签: jquery ajax json

我有一个让我疯狂的问题。通过JQuery我从服务器请求一个JSON文档,这是不被接受的(错误是“意外的字符”)。但是我的JSON文档看起来很好,在Firebug中,即正确检测和解析了JSON数据。

如果我复制响应并尝试通过jsonlint.com验证它,它总是在第一个字符([)上失败。如果我更改了一些字符并复制了一些内容但是结果相同则会验证(尝试复制[]的内容然后删除它们并再次写入它们,再次粘贴内容并拥有相同的文档但它现在验证)。奇怪。所以我认为它可能与编码有关,但是firefox明确指出所有内容都是UTF8编码的。

现在我有点无能为力,我希望你能以任何方式帮助我。

以下是我服务器的JSON响应:

[
    {
    "id": 67,
    "startdate": "01.01.1970 01:00:00",
    "hometeam": "EHC Muskrats",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 69,
    "startdate": "01.01.1970 01:00:00",
    "hometeam": "EC Mammuts",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 70,
    "startdate": "01.01.1970 01:00:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "EC Mammuts"
} ,         {
    "id": 86,
    "startdate": "01.01.1970 01:00:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "EC Mammuts"
} ,         {
    "id": 4,
    "startdate": "08.10.2012 21:00:00",
    "hometeam": "Wiener Teifl",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 18,
    "startdate": "28.10.2012 21:00:00",
    "hometeam": "EC Heizbären",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 19,
    "startdate": "04.11.2012 18:00:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "Innviertel Penguins"
} ,         {
    "id": 6,
    "startdate": "15.11.2012 21:00:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "EC Heizbären"
} ,         {
    "id": 62,
    "startdate": "17.11.2012 19:30:00",
    "hometeam": "EC V.U. Totonka",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 12,
    "startdate": "22.11.2012 20:30:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "EHC Muskrats"
} ,         {
    "id": 13,
    "startdate": "27.11.2012 19:30:00",
    "hometeam": "TÜV Cannibals",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 7,
    "startdate": "02.12.2012 17:00:00",
    "hometeam": "Fire Fighters jun.",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 63,
    "startdate": "03.12.2012 19:00:00",
    "hometeam": "",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 8,
    "startdate": "09.12.2012 17:00:00",
    "hometeam": "EHC Vienna Flames",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 14,
    "startdate": "17.12.2012 20:15:00",
    "hometeam": "EC Mammuts",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 15,
    "startdate": "07.01.2013 20:15:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "EC Mammuts"
} ,         {
    "id": 9,
    "startdate": "10.01.2013 20:00:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "EHC Chiefs"
} ,         {
    "id": 11,
    "startdate": "19.01.2013 21:00:00",
    "hometeam": "EC Attacki",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 16,
    "startdate": "24.01.2013 20:30:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "TÜV Cannibals"
} ,         {
    "id": 10,
    "startdate": "28.01.2013 19:30:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "EC V.U. Totonka"
} ,         {
    "id": 17,
    "startdate": "30.01.2013 20:30:00",
    "hometeam": "EHC Muskrats",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 65,
    "startdate": "24.02.2013 21:00:00",
    "hometeam": "EC Heizbären",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 66,
    "startdate": "28.02.2013 20:40:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "EHC Chiefs"
} ,         {
    "id": 64,
    "startdate": "08.03.2013 21:00:00",
    "hometeam": "EHC Vienna Flames",
    "awayteam": "EHC Vienna Wookies"
}     ]

3 个答案:

答案 0 :(得分:3)

JSON第一行的换行符有问题。如果您无法在服务器上修复它,请在解析javascript中的JSON响应之前尝试替换这些字符。

答案 1 :(得分:3)

在ID 67之前

,您在JSON中拥有.。如果你删除它。它会有效。虽然它没有出现在这里。但是如果你在这里尝试,你会在id 67之前看到一个额外的. (dot)Check the JSON. Remove . (dot) before the ID 67

  1. 在使用Json之前,在服务器端正确解析它。

答案 2 :(得分:0)

感谢Ravi指出我正确的方向。 JSON中有一个不可见的字符打破了JQuery JSON解析器。

服务器端的问题是我的所有文件中的1(!)编码为UTF8 + BOM: - <早就想到了。改变编码解决了它。