Rails 5 如何从 coffee 脚本文件获取 Ajax api 调用的响应到 rails 视图
Rails 5 How to get response of Ajax api call from coffee script file into a rails view
在我的 movies.coffee 文件中,我有这段代码,它正在调用 ajax 来获取电影列表。
$.ajax
url: 'https://api.themoviedb.org/3/genre/movie/list?api_key=someapikey',
success: (data) ->
response = data
然后我想从对某种 ruby 对象的调用中获取 json 响应,然后我可以循环遍历该对象并将其内容显示在 movies.index.html.erb 页面上多张 bootstrap 张卡片。
例如在@movies 实例变量中,我可以在其中调用@movies.title、@movies.genre 等等。
我无法将 api 数据附加到 bootstrap 卡片中,因为我还有其他数据也将显示在那里。
我想,您可以使用您的控制器索引方法。
你可以
begin
@movies = RestClient.get('https://api.themoviedb.org/3/genre/movie/list?api_key=someapikey')
rescue RestClient::ExceptionWithResponse => err
@movies = nil
@errors = err.response
end
您将在@movies 变量中获取数据,然后您可以将其填充到 html
在我的 movies.coffee 文件中,我有这段代码,它正在调用 ajax 来获取电影列表。
$.ajax
url: 'https://api.themoviedb.org/3/genre/movie/list?api_key=someapikey',
success: (data) ->
response = data
然后我想从对某种 ruby 对象的调用中获取 json 响应,然后我可以循环遍历该对象并将其内容显示在 movies.index.html.erb 页面上多张 bootstrap 张卡片。
例如在@movies 实例变量中,我可以在其中调用@movies.title、@movies.genre 等等。
我无法将 api 数据附加到 bootstrap 卡片中,因为我还有其他数据也将显示在那里。
我想,您可以使用您的控制器索引方法。 你可以
begin
@movies = RestClient.get('https://api.themoviedb.org/3/genre/movie/list?api_key=someapikey')
rescue RestClient::ExceptionWithResponse => err
@movies = nil
@errors = err.response
end
您将在@movies 变量中获取数据,然后您可以将其填充到 html