在jQuery中eval()的等价物是什么? [英] What's the equivalent but more idiomatic to eval( ) in jQuery?

查看:133
本文介绍了在jQuery中eval()的等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jQuery中进行此调用 eval('('+ responseText +')')相当于什么?我也明白 eval 并不安全,所以我在jQuery中寻找更安全,更惯用的东西。

What's the equivalent to making this call eval('('+responseText+')') in jQuery? I also understand that eval is not that safe, so I'm looking for something safer and more idiomatic in jQuery.

推荐答案

你真的想要执行Javascript代码(在某种程度上总是不安全),或者你是否试图解析 JSON

Are you actually looking to execute Javascript code (which will always be unsafe to some extent), or are you trying to parse JSON from a string into an object?

如果是前者,eval()仍然是你最好的选择。

If the former, eval() is still going to be your best bet.

如果是后者,jQuery将作为AJAX请求的一部分,或者您可以使用专用的 JSON插件在字符串和对象之间进行转换,而不存在执行任意代码的风险。

If the latter, jQuery will do it for you as part of an AJAX request, or you can use a dedicated JSON plug-in to convert between string and object without the risk of executing arbitrary code.

这篇关于在jQuery中eval()的等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆