带有base64编码字段的javascript eval json
我使用 sun.misc.BASE64Encoder 对加密值进行编码,然后将其添加到 JSON 字段,然后发送到客户端。我在客户端使用 Javascript 的 eval() 函数从 JSON 代码创建一个对象。当 eval() 运行时,它给出错误:
未终止的字符串文字
JSON 代码中还有其他字段,但我已将错误范围专门缩小到 base64 编码字段。这是有问题的 javascript 代码行:
var result = eval( '(' + xhr.responseText + ')' );
这是来自 Servlet 的 JSON 对象:
{
'resource':'resource?Signature=j79r/2Hly+HqhS/6fdd+prfsR+kUNijUvDN0QJ14ZR43gzYScOMDypt/crks/CEphTUXVptJvSol
1ZOOvScCUhNOCb7dZk/3MKnI5tOewSACXK32/OJNd8hYpZtSTn+WhA6+f9BUIUZWA83U8Cud/Tb8V
R1yQWbDGG/mM/NiUSiY=',
'url':'http://somesite.com/pr'
}
我不确定为什么 eval 会死掉,但似乎“资源”JSON 字段的值包含它不关心的内容。
提前致谢。 蒂姆
I am using sun.misc.BASE64Encoder to encode an encrypted value, which is then added to a JSON field and subsequently sent to the client. I use Javascript's eval() function on the client to create an object from the JSON code. When eval() runs, it gives the error:
unterminated string literal
There are other fields in the JSON code, but I've narrowed the error specifically to the base64 encoded field. Here's the offending line of javascript code:
var result = eval( '(' + xhr.responseText + ')' );
Here's the JSON object from the Servlet:
{
'resource':'resource?Signature=j79r/2Hly+HqhS/6fdd+prfsR+kUNijUvDN0QJ14ZR43gzYScOMDypt/crks/CEphTUXVptJvSol
1ZOOvScCUhNOCb7dZk/3MKnI5tOewSACXK32/OJNd8hYpZtSTn+WhA6+f9BUIUZWA83U8Cud/Tb8V
R1yQWbDGG/mM/NiUSiY=',
'url':'http://somesite.com/pr'
}
I'm not sure why eval is dying, but it seems the value of the 'resource' JSON field contains something it doesn't care for.
Thanks in advance.
Tim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这可能是因为您的 JSON 中似乎有换行符。如果你删除它们,它会起作用吗?
I think it may be because your JSON appears to have line breaks in it. If you remove them, does it work?