My Spider Sense警告我,使用eval()
解析传入的JSON是一个坏主意.我只是想知道JSON.parse()
- 我认为它是JavaScript的一部分而不是浏览器特定的功能 - 更安全.
jld*_*ont 109
如果使用eval
以下内容,您更容易受到攻击:JSON是Javascript的一个子集,json.parse只解析JSON,但eval
会对所有JS表达式敞开大门.
plo*_*der 37
所有JSON.parse
实现最有可能使用eval()
JSON.parse
基于Douglas Crockford的解决方案,该解决方案eval()
在第497行使用.
// In the third stage we use the eval function to compile the text into a
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
// in JavaScript: it can begin a block or an object literal. We wrap the text
// in parens to eliminate the ambiguity.
j = eval('(' + text + ')');
Run Code Online (Sandbox Code Playgroud)
优点JSON.parse
是它验证了参数是正确的JSON语法.
Aut*_*ter 14
并非所有浏览器都具有本机JSON支持,因此有时您需要使用eval()
JSON字符串.使用来自http://json.org的 JSON解析器,因为它可以更轻松地处理所有事情.
Eval()
是一种邪恶,但对某些浏览器来说它是一个必要的邪恶,但你可以避免它,这样做!!!!!
Jef*_*ery 11
JSON.parse()和eval()将接受的区别.试试eval:
var x ="{\"shoppingCartName \":\"shopping_cart:2000 \"}"
eval(x) //won't work
JSON.parse(x) //does work
Run Code Online (Sandbox Code Playgroud)
看这个例子.
归档时间: |
|
查看次数: |
114384 次 |
最近记录: |