【问题标题】:Unexpected token ILLEGAL javascript error in Google ChromeGoogle Chrome 中出现意外的令牌 ILLEGAL javascript 错误
【发布时间】:2011-01-10 13:45:02
【问题描述】:

我收到一个 javascript (prototype.js) 错误:这行代码出现意外令牌非法:

newFriend = new friend(
    response[0].@items[0]._id, 
    response[0].@items[0]._nickName, 
    response[0].@items[0]._profilePicture, 
    response[0].@items[0]._tagLine, 
    response[0].@items[0]._isInvite, 
    response[0].@items[0]._confirm
);

响应对象如下所示:

[{
  "@type": "[Lcom.photoviewer.common.model.ThinUser;",
  "@items": [{
    "_id": "000.060318.05022007.00263.0067ur",
    "_nickName": "siraj",
    "_country": null,
    "_currentStorageLimit": 5000000000,
    "_currentStorage": 0,
    "_currentFileCount": 0,
    "_profilePicture": null,
    "_tagLine": null,
    "_membershipLevel": 0,
    "_isRejected": false,
    "_isInvite": false,
    "_confirm": false,
    "_verifiedOn": 1170716666000
  }]
}]

这只发生在 Google Chrome 浏览器和可能的其他 webkit 浏览器中。它在 Firefox 中运行良好。

【问题讨论】:

    标签: javascript json google-chrome webkit


    【解决方案1】:

    试试这个:

    newFriend = new friend(
        response[0]["@items"][0]._id, 
        response[0]["@items"][0]._nickName, 
        response[0]["@items"][0]._profilePicture, 
        response[0]["@items"][0]._tagLine, 
        response[0]["@items"][0]._isInvite, 
        response[0]["@items"][0]._confirm
    );
    

    我很确定 @ 给你带来了麻烦。

    对于奇怪的字符,使用["@items"] 表示法而不是(点)表示法.@items 总是更安全。

    【讨论】:

      【解决方案2】:

      包含 @ 和点符号的属性名称在 Chrome 中不兼容。请改用square bracket notation(在构造对象时已经这样做了)。

      【讨论】:

      • 哇,我很惊讶它可以在任何地方工作! @ 绝对不允许在 JS 标识符中使用(根据 ECMA-262-3 等)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      相关资源
      最近更新 更多