【发布时间】:2018-09-16 22:15:32
【问题描述】:
我之前看到有人问过这个问题,但答案对我没有帮助。
我有一个使用我自己构建的 Rails 5.2 api 的 ionic 移动应用程序。 我无法通过 PUT 请求。 我遇到了这个错误。
Failed to load https://votingapi.herokuapp.com/api/v1/something: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.
我已经向 API 发出了 GET 请求,没有任何问题。
通过启用 chrome 的 Allow-Control-Allow-Origin: * 插件,设法解决了 ionic serve --lab 中的问题,但问题仍然存在于 Ionic 视图中。
有什么想法吗?
宝石文件
gem 'rack-cors', require: 'rack/cors'
应用程序.rb
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*',
headers: :any,
methods: %w[:get :post :put :delete],
expose: %w[access-token expiry token-type uid client],
max_age: 0
end
对离子的请求
update(id, result){
let headers = new Headers();
let body = JSON.stringify({vote: result})
headers.append("Accept" , 'application/json');
headers.append('Content-Type', 'application/json');
let options = new RequestOptions({ headers: headers });
return this.http.put(`${this.apiUrl}/${id}`, body, options)
}
【问题讨论】:
标签: http ionic-framework cors rails-api ionic-view