2010-01-28 76 views
4

我得到一个JavaScript(prototype.js中)错误:意外的令牌在这行代码非法的:意外标记非法JavaScript错误

newFriend = new friend(
    response[0][email protected][0]._id, 
    response[0][email protected][0]._nickName, 
    response[0][email protected][0]._profilePicture, 
    response[0][email protected][0]._tagLine, 
    response[0][email protected][0]._isInvite, 
    response[0][email protected][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中正常工作。

回答

8

试试这个:

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"]符号而不是(点)符号[email protected]总是更安全。

0

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

+5

哇,我很惊讶它可以在任何地方工作! JS标识符中绝对不允许使用“@”(根据ECMA-262-3等)。 – bobince 2010-01-28 21:22:24