[英]Serve JSON from index action to index.html in public folder
我正在尝试使用Rails作为后端创建一个单页应用程序,因为这是我所知道的唯一后端框架。 我将所有文件放在公用文件夹中,还安装了npm和jspm,以便可以使用javascript功能。
我想做的是在我的电影控制器中执行此索引操作
def index
@movies = Movie.all
render :json => @movies
end
将电影的集合作为JSON数据发送到位于公共文件夹中的index.html文件。 我正在使用“提取”功能来检索我的client.js文件中的数据:
let api_url = 'http://127.0.0.1:3000'
fetch(api_url).then(function(resp) {
resp.json().then(function(movies) {
console.log(movies)
})
})
这将导致以下错误:
Uncaught (in promise) SyntaxError: Unexpected token <
我不确定这意味着什么,甚至不确定是否正确。 任何帮助将不胜感激。
更改为此:
let api_url = 'http://127.0.0.1:3000/movies'
两者相同:
fetch(api_url).then(function(resp) {
resp.json().then(function(movies) {
console.log(movies)
})
})
要么
fetch(api_url).then(r => r.json())
.then(data => console.log(data))
.catch(e => console.log('error'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.