将JSON对象从rails控制器发送到Javascript

时间:2015-10-12 03:37:19

标签: javascript ruby-on-rails json spotify

所以我有一个我建立的哈希,看起来像这样:

Track_list = {:track1=>{:url=>"https://open.spotify.com/track/2Oehrcv4Kov0SuIgWyQY9e", :name=>"Demons"},
 :track2=>{:url=>"https://open.spotify.com/track/0z8yrlXSjnI29Rv30RssNI", :name=>"Shots - Broiler Remix"},
 :track3=>{:url=>"https://open.spotify.com/track/6Ep6BzIOB9tz3P4sWqiiAB", :name=>"Radioactive"},
 :track4=>{:url=>"https://open.spotify.com/track/3I05foFixB2sSZvV5Ppty8", :name=>"Blank Space/Stand By Me - Medley / Live From Spotify London"},
 :track5=>{:url=>"https://open.spotify.com/track/4G8gkOterJn0Ywt6uhqbhp", :name=>"Radioactive"}}

我正在尝试将该哈希转换为JSON,因此我可以在我的JS文件中使用它,这就是我在我的控制器中所做的:

@tl = track_list.as_json
# and it produces a result like this:
# {"track1"=>{"url"=>"https://open.spotify.com/track /2Oehrcv4Kov0SuIgWyQY9e", "name"=>"Demons"},
# "track2"=>{"url"=>"https://open.spotify.com/track/0z8yrlXSjnI29Rv30RssNI", "name"=>"Shots - Broiler Remix"},
# "track3"=>{"url"=>"https://open.spotify.com/track/6Ep6BzIOB9tz3P4sWqiiAB", "name"=>"Radioactive"},
# "track4"=>{"url"=>"https://open.spotify.com/track/3I05foFixB2sSZvV5Ppty8", "name"=>"Blank Space/Stand By Me - Medley / Live From Spotify London"},
# "track5"=>{"url"=>"https://open.spotify.com/track/4G8gkOterJn0Ywt6uhqbhp", "name"=>"Radioactive"}}

然而,当我转到我的JS文件并尝试打印出文件时,这就是我得到的:

console.log("<%= @tl %>");
"{&quot;track1&quot;:{&quot;url&quot;:&quot;https://open.spotify.com/track/2Oehrcv4Kov0SuIgWyQY9e&quot;,&quot;name&quot;:&quot;Demons&quot;},&quot;track2&quot;:{&quot;url&quot;:&quot;https://open.spotify.com/track/0z8yrlXSjnI29Rv30RssNI&quot;,&quot;name&quot;:&quot;Shots - Broiler Remix&quot;},&quot;track3&quot;:{&quot;url&quot;:&quot;https://open.spotify.com/track/6Ep6BzIOB9tz3P4sWqiiAB&quot;,&quot;name&quot;:&quot;Radioactive&quot;},&quot;track4&quot;:{&quot;url&quot;:&quot;https://open.spotify.com/track/3I05foFixB2sSZvV5Ppty8&quot;,&quot;name&quot;:&quot;Blank Space/Stand By Me - Medley / Live From Spotify London&quot;},&quot;track5&quot;:{&quot;url&quot;:&quot;https://open.spotify.com/track/4G8gkOterJn0Ywt6uhqbhp&quot;,&quot;name&quot;:&quot;Radioactive&quot;}}"

当我尝试做的时候:

console.log(JSON.parse("<%= @tl %>");

它不起作用。我还尝试将j放在@tl前面,它给了我一个错误,说哈希不能和gsub一起使用。

此外,当我尝试这样做时:

@track_list = JSON.generate(@tl) # from above

我明白了:

"{\"track1\":{\"url\":\"https://open.spotify.com/track/2Oehrcv4Kov0SuIgWyQY9e\",\"name\":\"Demons\"},\"track2\":{\"url\":\"https://open.spotify.com/track/0z8yrlXSjnI29Rv30RssNI\",\"name\":\"Shots - Broiler Remix\"},\"track3\":{\"url\":\"https://open.spotify.com/track/6Ep6BzIOB9tz3P4sWqiiAB\",\"name\":\"Radioactive\"},\"track4\":{\"url\":\"https://open.spotify.com/track/3I05foFixB2sSZvV5Ppty8\",\"name\":\"Blank Space/Stand By Me - Medley / Live From Spotify London\"},\"track5\":{\"url\":\"https://open.spotify.com/track/4G8gkOterJn0Ywt6uhqbhp\",\"name\":\"Radioactive\"}}"

所以它实际上正确地逃避了一切......我不知道为什么当我在我的JS文件中使用相同的变量时,它并没有逃脱引号或任何东西。

谢谢!

2 个答案:

答案 0 :(得分:2)

您可以通过以下方式将Rails控制器中的JSON对象发送到Javascript:

console.log("<%= @tl.to_json.html_safe %>");

答案 1 :(得分:0)

试试这个:

var my_obj = <%= j @tl %>;
console.log(my_obj);

你只有几个小问题。您需要在视图中使用escape_javascript(或其别名j)。但是你也在引号之间插入json对象,这将导致浏览器将其视为字符串而不是json对象。