Rails 中奇怪的 JSON Javascript 问题

发布于 2024-11-06 13:08:51 字数 1502 浏览 1 评论 0原文

我正在尝试将 JSON 从我的控制器获取到我的视图。在我的控制器中,我正在做:

@nodes = Node.all
@json = @nodes.as_json(:only => [:ID, :Lat, :Lon]) 

在我看来,我已经尝试过:

1) var stuff = <%= @json %>
2) var stuff = <%= @json.to_json %>
3) var stuff = <%= @json.to_json.to_json %>

所有这些都给了我一个错误。我通常会收到“意外的语法错误&”或“意外的语法错误{”

我也尝试过使用jquery并在控制器内使用respond_to,但这似乎也不起作用。

我的想法是,将 json 获取到视图不应该是一个大问题,也不应该需要 jQuery,目前,我的页面源代码如下所示:

var stuff = [{&quot;node&quot;:{&quot;ID&quot;:1301499692582,&quot;Lat&quot;:42.3605063113369,&quot;Lon&quot;:-71.0870862191138}},{&quot;node&quot;:{&quot;ID&quot;:1301499691515,&quot;Lat&quot;:42.3605147089149,&quot;Lon&quot;:-71.0870533282532}},{&quot;node&quot;:{&quot;ID&quot;:1301431075499,&quot;Lat&quot;:42.3605456103,&quot;Lon&quot;:-71.0875239075536}} etc

我不明白 &quot 符号(也许这就是语法错误的来源) )但是当我渲染时:json => @nodes.to_json,页面呈现一个有效的普通 json:

[{"node":{"ID":1301499692582,"Lat":42.3605063113369,"Lon":-71.0870862191138}},{"node":{"ID":1301499691515,"Lat":42.3605147089149,"Lon":-71.0870533282532}},{"node":{"ID":1301431075499,"Lat":42.3605456103,"Lon":-71.0875239075536}}

注意:我也尝试过这样做 var stuff = '<%= @json.to_json %>;但是当我执行 var json = JSON.parse(stuff) 时,它给了我一个非法令牌错误。

有人可以帮我解决这个问题吗?非常感谢!

I'm trying to get my JSON from my controller to my view. In my controller I am doing:

@nodes = Node.all
@json = @nodes.as_json(:only => [:ID, :Lat, :Lon]) 

In my view I have tried:

1) var stuff = <%= @json %>
2) var stuff = <%= @json.to_json %>
3) var stuff = <%= @json.to_json.to_json %>

and all of those give me an error. I usually get an "Unexpected Syntax Error &" or "Unexpected Syntax Error {"

I have also tried using jquery and using respond_to within the controller, but that doesn't seem to work either.

My thoughts are that getting json to the view shouldn't be a big issue and shouldn't require jQuery, and currently, my page source looks like:

var stuff = [{"node":{"ID":1301499692582,"Lat":42.3605063113369,"Lon":-71.0870862191138}},{"node":{"ID":1301499691515,"Lat":42.3605147089149,"Lon":-71.0870533282532}},{"node":{"ID":1301431075499,"Lat":42.3605456103,"Lon":-71.0875239075536}} etc

I dont understand the " symbols (maybe thats where the syntax error is coming from) but when I do render :json => @nodes.to_json, the page renders a normal json that is valid:

[{"node":{"ID":1301499692582,"Lat":42.3605063113369,"Lon":-71.0870862191138}},{"node":{"ID":1301499691515,"Lat":42.3605147089149,"Lon":-71.0870533282532}},{"node":{"ID":1301431075499,"Lat":42.3605456103,"Lon":-71.0875239075536}}

Note: I've also tried doing var stuff = '<%= @json.to_json %> but when I do var json = JSON.parse(stuff), it gives me an illegal token error.

Can someone please help me with this? Thanks so much!

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

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

发布评论

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

评论(2

比忠 2024-11-13 13:08:51

这是 Rails 3 中默认对字符串进行 html 编码。

您需要将 JSON 标记为 html_safe

var stuff = <%= @json.to_s.html_safe %>

请注意,需要 .to_s,因为 as_json< /code> 给出哈希而不是字符串。你可以这样做:

# in controller
@json = @nodes.to_json(:only => [:ID, :Lat, :Lon]) 

#and in view
var stuff = <%= @json.html_safe %>

This is Rails html-encoding your string as is default in Rails 3.

You need to mark your JSON as html_safe:

var stuff = <%= @json.to_s.html_safe %>

Note that .to_s is needed because as_json gives Hash instead of string. You could do this instead:

# in controller
@json = @nodes.to_json(:only => [:ID, :Lat, :Lon]) 

#and in view
var stuff = <%= @json.html_safe %>
只为守护你 2024-11-13 13:08:51

我认为你需要在它周围加上引号,然后你可以让 jquery 将字符串解析为 JSON。

I think you need to put quotes around it, then you can ask jquery to parse the string into JSON.

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