Rails 中奇怪的 JSON Javascript 问题
我正在尝试将 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 = [{"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
我不明白 " 符号(也许这就是语法错误的来源) )但是当我渲染时: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 Rails 3 中默认对字符串进行 html 编码。
您需要将 JSON 标记为
html_safe
:请注意,需要
.to_s
,因为as_json< /code> 给出哈希而不是字符串。你可以这样做:
This is Rails html-encoding your string as is default in Rails 3.
You need to mark your JSON as
html_safe
:Note that
.to_s
is needed becauseas_json
gives Hash instead of string. You could do this instead:我认为你需要在它周围加上引号,然后你可以让 jquery 将字符串解析为 JSON。
I think you need to put quotes around it, then you can ask jquery to parse the string into JSON.