$.each() 迭代字符串中的字符 - 我需要 eval() 吗?

发布于 2024-08-30 21:28:34 字数 217 浏览 1 评论 0原文

我有 json 数据作为集合返回:

var foo = ["6", "7", "33"]

在 jQuery 中使用 JSONP。由于我使用的是 JSONP,数据将返回到回调函数,该函数将其解释为字符串而不是集合。在将回调中的字符串作为集合处理之前,我是否需要对其运行 eval(foo) ,或者是否有其他方法可以对其进行重铸?

I have json data being returned as a collection:

var foo = ["6", "7", "33"]

using JSONP in jQuery. Since I'm using JSONP, the data is being returned to a callback function, which is interpreting it as a string instead of a collection. Do I need to run eval(foo) on the string in the callback before handling it as a collection, or is there some other means of recasting it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟织青萝梦 2024-09-06 21:28:34

如果您使用的是 jQuery,则应该使用 dataType: "jsonp" 执行 .ajax,这应该将解析后的数据发送到您的回调,而不是字符串。如果这对您不起作用,或者还有我没有看到的其他复杂性,您可以使用 $.parseJSON(foo) 而不是 eval(foo)。如果存在,这将调用浏览器的本机 JSON.parse 方法,如果不存在,则使用 eval

If you're using jQuery, you should be doing .ajax with dataType: "jsonp", which should send the parsed data to your callback, rather than a string. If this doesn't work for you, or there are other complexities I'm not seeing, you can use $.parseJSON(foo) instead of eval(foo). This will call the browser's native JSON.parse method if it exists, or use eval if it doesn't.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文