gpt4 book ai didi

javascript - 将 JSON 对象从 Rails Controller 发送到 Javascript

转载 作者:行者123 更新时间:2023-11-29 21:40:36 24 4
gpt4 key购买 nike

所以我构建了一个散列,看起来像这样:

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 文件中使用它,这就是我在我的 Controller 中所做的:

@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 前面,它给了我一个错误,指出 hash 不能与 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 文件中使用相同的变量时,它没有转义引号或任何东西。

谢谢!

最佳答案

您可以通过这样做将 JSON 对象从 Rails Controller 发送到 Javascript:

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

关于javascript - 将 JSON 对象从 Rails Controller 发送到 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33072903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com