IE8 -if JSONP 数组在逗号后没有最后一个元素 ..get 错误
我使用 JSONP 的 X 站点脚本在 IE8 上遇到错误,但在 firefox 和 crome 上运行良好... 这里返回的 JSON 对象传递一个末尾有逗号且没有最后一个元素的数组...即 类似 {a,b,c, } 的东西...如您所见最后一个元素后面有一个逗号,这就是在 IE 上引起问题的原因。 实际的 JSONP 响应是: gotMsg({"msg":[{"lastMsgId":"69","user":"diptanu","textVal":"Sup","time":"2010-09-04 06:20:15", ...正如您所看到的,最后一个逗号是导致问题的原因...错误是
预期标识符、字符串或数字 chat.php?callback=gotMsg&user=&lastMsgId =-1&ref=1&userId=-1&loc=http%3A%2F%2Faagmgyd6.yahoo.joyent.us%2Fchat%2Findex.html,第 1 行字符 96....
请帮忙 谢谢 莫汉·古普塔
My X-site scripting using JSONP encounters error on IE8 but works fine on both firefox and crome...
here the returned JSON object passes an array with a comma at the end and no last element ...i.e something like {a,b,c, } ..as you can see the last element is succeeded by a comma and that is what is causing problem on IE.
the actual JSONP response is :
gotMsg({"msg":[{"lastMsgId":"69","user":"diptanu","textVal":"Sup","time":"2010-09-04 06:20:15",} ...as you can see the last comma is what it says is causing problem...the error is
Expected identifier, string or number chat.php?callback=gotMsg&user=&lastMsgId=-1&ref=1&userId=-1&loc=http%3A%2F%2Faagmgyd6.yahoo.joyent.us%2Fchat%2Findex.html, line 1 character 96....
Please help
thanks
Mohan Gupta
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JSON 编码器通常不添加逗号,PHP 的
json_encode
则不会。在 JavaScript 中,您不应该在数组的最后一个元素后添加逗号,但 Firefox 和 Chrome 接受这一点。尝试使用 json-encoder 函数,或者如果不可能,请进行一些字符串处理以删除最后一个逗号。
JSON encoders don't usually add a comma, PHP's
json_encode
doesn't. In JavaScript you shouldn't add a comma after array's last element, but firefox and chrome accept that.Either try your json-encoder function, or if it's not possible, do some string processing to strip the last comma.
为了避免与浏览器严重不兼容,请使用 Aptana Studio 或 Eclipse/Javascript 编辑器。它们都可以验证服务器脚本生成的 Javascript 和 JSON 对象。
以下是正确构造个人数据 JSON 对象的示例。您可以从 WIKI::JavaScript Object Notation 获取它
当您从以下对象生成 JSON 时PHP 你最好使用 json_encode 方法或不同语言中的等效方法在您遇到错误之前生成正确编码的 JSON 结构。
To avoid major incompatibility with browser use Aptana Studio or Eclipse/Javascript Editor. They both have possibility to validate the Javascript and JSON objects generated by your server scripts.
Here is the example of correctly constructed JSON object of the personal data. You may get it from WIKI::JavaScript Object Notation
When you generate the JSON from an object in PHP you'll better use json_encode method or equivalent in different languages which produces the correctly encoded JSON structure prior to the errors you encountered.
IE8 是唯一正确抛出错误的浏览器:根据 JSONLint,这是无效的 JSON。
你必须以某种方式修复源代码,我看不到任何方法让它在 IE 中工作。
IE8 is the only browser to correctly throw an error: This is invalid JSON according to JSONLint.
You would have to get the source fixed somehow, I can't see any way of getting this to work in IE.