【发布时间】:2015-01-27 11:07:38
【问题描述】:
我正在尝试使用 Ember-CLI (ember.js) 和 Rails 4.2.0 构建应用程序。我将 Ember 和 Rails 代码库分开,所以我使用 rack-cors gem 启用了 CORS。当 Ember 应用程序向 Rails api 发出请求时,我不断收到以下错误:
406 Not Acceptable
还有:
No 'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost:4200' is therefore not allowed access.
这在 Rails 方面:
Processing by CustomersController#index as HTML
Completed 406 Not Acceptable in 2ms
ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/customers_controller.rb:9:in `index'
请求Accept头是:
Accept:application/json, text/javascript, */*; q=0.01
我的控制器操作是:
def index
@customers = Customer.all
respond_to do |format|
format.json { render json: @customers }
#format.js { render json: @customers } Tried this too
end
end
我还有一个使用 active_model_serializer gem 的 customer_serializer。
任何帮助将不胜感激!
【问题讨论】:
-
我从未使用过
rack-cors,但它似乎不起作用。 Rails 的响应中实际上是否有Access-Control-Allow-Origin标头? -
没有 :defaults => { :format => :json } 在我的客户路线上,标题没有被设置。这解决了它。不太明白为什么。
标签: ruby-on-rails ruby-on-rails-4 ember.js