IE6 中嵌套 JSON 会导致问题
我使用 jQuery 的 $.getJSON
方法向 JSON 页面发出请求,并根据返回的 JSON 创建一些 HTML 并将其放在页面上。
当我有一个嵌套的 JSON 对象时,就会出现问题,稍后我将向您展示一个示例。
首先,如果我向 JSON 页面发出请求并返回以下 JSON,则该函数工作正常,并且我看到页面上出现一个漂亮的 HTML 元素:
JSON:
({
"variants": [
{
"variantId": "536",
"title": "Party Like a Rock Star for Two at the Metropolitan hotel, London ",
"price": "£299.00"
}
]
})
工作正常,没有错误。
但是,一旦我返回下面的 JSON,该函数就不起作用。
({
"variants": [
{
"variantId": "536",
"title": "Party Like a Rock Star for Two at the Metropolitan hotel, London ",
"price": "£299.00",
"blogs": [
{
"title": "Another test",
"author": "Sean",
},
{
"title": "This is a test",
"author": "Sean",
}
]
}
]
})
正如您所看到的,其中没有任何字符会导致它太损坏。我也尝试过重命名字段,只是碰巧“博客”、“标题”或“作者”是 JS 中的保留字(正如我所想,没有区别!)
以确保这不是我处理导致问题的数据,我发出警报(“到了这里。”);作为我的 $.getJSON 函数中的第一段代码(见下文),它不会触发,所以我知道这不是我对数据所做的操作导致错误。
$.getJSON('/ajax/cover_flow_detail.ashx?experienceId=' + arguments[0], function(d) {
alert('Got here'); // doesn't fire ?
// omitted for brevity.
}
更奇怪的是——这只发生在 IE6 中。 IE7和IE7 FF 还好。
任何朝正确方向的推动将不胜感激,我完全被难住了!
干杯,肖恩
I'm making a request to a JSON page using jQuery's $.getJSON
method, and from the returned JSON i'm creating some HTML and putting it on to the page.
The problems appears when I have a nested JSON object, i'll show you an example later.
First off, if I make a request to my JSON page and return the following JSON, the function works just fine and i see a beautiful HTML element appear on the page:
JSON:
({
"variants": [
{
"variantId": "536",
"title": "Party Like a Rock Star for Two at the Metropolitan hotel, London ",
"price": "£299.00"
}
]
})
This works fine, no errors.
However, as soon as i return the JSON below, the function does not work.
({
"variants": [
{
"variantId": "536",
"title": "Party Like a Rock Star for Two at the Metropolitan hotel, London ",
"price": "£299.00",
"blogs": [
{
"title": "Another test",
"author": "Sean",
},
{
"title": "This is a test",
"author": "Sean",
}
]
}
]
})
As you can see, there are no characters in it that would cause it too break. I've also tried renaming the fields, just by chance that "blogs", "title", or "author" were reserved words in JS (as i thought, no difference!)
To make sure it was not my way of processing the data that was causing a problem, i stuck an alert('Got here.'); as the first piece of code (see below) in my $.getJSON function, and that doesn't fire so I know it's not what i'm doing with the data that is causing an error.
$.getJSON('/ajax/cover_flow_detail.ashx?experienceId=' + arguments[0], function(d) {
alert('Got here'); // doesn't fire ?
// omitted for brevity.
}
Even weirder - this is only happening in IE6. IE7 & FF are fine.
Any push in the right direction would be appreciated, i'm completely stumped!
Cheers, Sean
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 JSON 中有错误 — 某些对象定义中存在尾随逗号。
(这表明您正在使用模板而不是 JSON 库生成 JSON,这是一个错误。)
与其他浏览器相比,IE 不太容忍该错误。
You have an error in your JSON — trailing commas in some of your object definitions.
(This suggests you are generating your JSON using a template instead of a JSON library, this is a mistake.)
IE is less forgiving of that error than other browsers.
Internet Explorer 因中断尾随逗号而臭名昭著。
在 IE 上失败,而其他浏览器会忽略第二个元素后面的尾随逗号。
Internet Explorer is notorious for breaking on trailing commas.
Fails on IE, while other browsers ignore the trailing comma after the second element.
讽刺的是,它应该是错误的。 IE 做对了。如果语法不正确,浏览器不应该松散地解析 JSON。 , 应被视为 ;然后。
我相信它是解析器,因为在 EXT JS 中,它是严格的。
请记住,封闭不良的 HTML 标记会被 IE 忽略,而不是 Netscape。有趣的是转身。
Ironically, it should be erroring. IE did it right. Browsers shouldn't loosely parse JSON if the syntax isn't right. The , should be treated like a ; then.
I believe it is the parser, because in EXT JS, it is strict.
Remember when HTML tags that were poorly closed would be ignored by IE, and not Netscape. Interesting in the turn around.