IE8 -if JSONP 数组在逗号后没有最后一个元素 ..get 错误

发布于 2024-09-17 12:45:09 字数 553 浏览 4 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一曲琵琶半遮面シ 2024-09-24 12:45:10

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.

橙味迷妹 2024-09-24 12:45:10

为了避免与浏览器严重不兼容,请使用 Aptana StudioEclipse/Javascript 编辑器。它们都可以验证服务器脚本生成的 Javascript 和 JSON 对象。

以下是正确构造个人数据 JSON 对象的示例。您可以从 WIKI::JavaScript Object Notation 获取它

{
     "firstName": "John",
     "lastName": "Smith",
     "age": 25,
     "address": {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": "10021"
     },
     "phoneNumber": [
         {
           "type": "home",
           "number": "212 555-1234"
         },
         {
           "type": "fax",
           "number": "646 555-4567"
         }
     ]
}

当您从以下对象生成 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

{
     "firstName": "John",
     "lastName": "Smith",
     "age": 25,
     "address": {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": "10021"
     },
     "phoneNumber": [
         {
           "type": "home",
           "number": "212 555-1234"
         },
         {
           "type": "fax",
           "number": "646 555-4567"
         }
     ]
}

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.

笑叹一世浮沉 2024-09-24 12:45:10

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文