背景:
我使用"rails g scaffold hotel name stars:integer"来快速启动(并在数据库中插入一些记录),并在rails app之外编写一个Backbone客户端.
我使用Safari文件在本地打开Backbone客户端:///Users/lg/Workspace/www/index.html用于测试客户端,因为我的想法是将rails服务器放在主机上(例如Heroku)并插入Backbone客户端进入PhoneGap应用程序.
我的骨干客户端只有几行:
Hotel = Backbone.Model.extend({
initialize: function(){
console.log("initialize Hotel")
}
});
Hotels = Backbone.Collection.extend({
model: Hotel,
url: 'http://0.0.0.0:3000/hotels'
});
Run Code Online (Sandbox Code Playgroud)
但是,当我与骨干取酒店,导轨与响应format.html而不是format.json其中骨干可以解析.
hotels_controller.rb
# GET /hotels
# GET /hotels.json
def index
@hotels = Hotel.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @hotels }
end
end
Run Code Online (Sandbox Code Playgroud)
Safari检查器控制台:
hotels = new Hotels()
Object
hotels.fetch()
Object
hotels.length
0
Request URL:http://0.0.0.0:3000/hotels
Request method:GET
Status code:200 OK
Request Headers
Accept:application/json, text/javascript, */*; …Run Code Online (Sandbox Code Playgroud)