我想使用 Angular.js 和 Rails 显示卡片索引。
这是我的代码。
# app/views/decks/show.html.erb
<div ng-controller="DeckCtrl">
<form ng-submit="addCard()">
<input type="text" ng-model="newCard.front" />
<input type="submit" value="Add">
</form>
{{newCard.front}}
<ul>
<li ng-repeat="card in cards">
{{card.front}}
</li>
</ul>
</div>
和 CoffeeScript
# app/assets/javascripts/decks.js.coffee
app = angular.module("Deck", ["ngResource"])
@DeckCtrl = ($scope, $resource) ->
Card = $resource("/cards/:id", {id: "@id"}, {update: {method: "PUT"}})
$scope.cards = Card.query()
$scope.addCard = ->
$scope.cards.push($scope.newCard)
$scope.newCard = {}
和CardsController
。
class CardsController < ApplicationController
respond_to :json
def index
respond_with Card.all
end
def show
respond_with Card.find(params[:id])
end
def create
respond_with Card.create(card_params)
end
def update
respond_with Card.update(params[:id], card_params)
end
def destroy
respond_with_Card.destroy(params[:id])
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def card_params
params.require(:card).permit(:front, :back, :tips, :cardtype, :deck_id, :box_number)
end
end
addCard
函数正在运行,但是 Card.query()
给我这个错误。
Started GET "/cards" for 127.0.0.1 at 2014-04-17 10:39:42 +0900
Processing by CardsController#index as HTML
Completed 406 Not Acceptable in 2ms
ActionController::UnknownFormat - ActionController::UnknownFormat:
看起来 Angular.js
发送的是 HTML
而不是 json
,这会导致错误,对吗?
我怎样才能消除这个错误?
最佳答案
406表示服务器不支持客户端请求的格式。
angular默认发送的Accept
头是application/json, text/plain, */*
。所以基本上它接受一切。 $resource
在我看来没有任何改变。
很可能在您的应用程序的某处设置了您的服务器无法处理的格式。
关于javascript - 尝试 Angular.js query() 函数时出现 406 Not Acceptable 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23123184/