Tag: cors

Rails,REST架构和HTML 5:具有飞行前请求的跨域请求

在开展项目以使我们的网站HTML 5友好时,我们渴望采用跨域请求的新方法(不再通过隐藏的iframe发布!!!)。 使用访问控制规范,我们开始设置一些测试来validation各种浏览器的行为。 当前的Rails RESTful架构依赖于四个HTTP谓词:GET,POST,PUT,DELETE。 但是,在访问控制规范中,它规定非简单方法(PUT,DELETE)需要使用HTTP谓词OPTIONS的飞行前请求。 此外,在测试期间,我们还发现了Firefox 3.5.8的飞行前POST请求。 我的问题是这个。 是否有人知道Rails框架的任何项目正在解决这个问题? 如果没有,有关支持OPTIONS方法的最佳策略的任何意见,因为它必须支持所有POST,PUT,DELETE方法的路由?

由于Rails / AJAX应用程序上的CORS,无法在本地访问IBM Watson API

似乎没有很多关于如何处理这个问题的答案(但很多问题),所以我要将我的名字添加到合唱中并祈祷一个不涉及Node的答案。 我的错误来自Chrome控制台: 1. POST https://gateway.watsonplatform.net/visual-recognition-beta/api 2. XMLHttpRequest cannot load https://gateway.watsonplatform.net/visual-recognition-beta/api. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:3000’ is therefore not allowed access. The response had HTTP status code 401. 我正在使用Rails AJAX请求: $.ajax({ method: “POST”, version: ‘v2-beta’, url: “https://gateway.watsonplatform.net/visual-recognition-beta/api”, password: “———–“, username: “———–“, version_date:’2015-12-02′, visual_recognition: [ { name: “visual-recognition-service”, label: “visual_recognition”, plan: […]

Rails Passenger Glyphicon CORS Cloudfront NGINX问题

所以我知道stackoverflow是关于CORS Nginx,Cloudfront和Heroku的这些问题,但由于某种原因,我无法让它工作。 我一直在关注这个问题的答案: 如何使用rails,nginx和passenger配置`Access-Control-Allow-Origin`? 但是,我似乎无法弄清楚自定义代码块的放置位置: config/nginx.conf.erb 接下来,通过查找如下所示的块来编辑配置文件config / nginx.conf.erb: location @static_asset { gzip_static on; expires max; add_header Cache-Control public; add_header ETag “”; } …and add the two Access-Control lines: 至 > location @static_asset { > gzip_static on; > expires max; > add_header Cache-Control public; > add_header ETag “”; > add_header Access-Control-Allow-Origin *; > add_header Access-Control-Request-Method […]

如果用户未经授权,如何使用Devise发送CORS头(401响应)

我正在开发一个应用程序,其中服务器和api消费客户端位于不同的域下,所以我想使用CORS 。 为此,我必须在服务器响应中设置相应的http标头: def cors_set_access_control_headers headers[‘Access-Control-Allow-Origin’] = ‘http://localhost’ headers[‘Access-Control-Allow-Methods’] = ‘POST, GET, OPTIONS’ headers[‘Access-Control-Allow-Headers’] = ‘*, X-Requested-With, X-Prototype-Version, X-CSRF-Token, Content-Type’ headers[‘Access-Control-Max-Age’] = “1728000” end 此方法用作ApplicationController的before_filter 。 对于某些资源,用户必须经过身份validation和授权。 请求通过XHR / Ajax完成。 因此,如果用户未经过身份validation, Devise将向客户端发送401响应,而不是重定向到登录页面。 但是用于设置CORS头的filter不用于该响应。 因此,401响应不会发送给客户端。 我想在客户端捕获并使用401响应。 目前我使用的解决方法是不使用Devise身份validation方法,而是使用自定义身份validation代码段: def authenticate_cors_user if request.xhr? && !user_signed_in? error = { :error => “You must be logged in.” } render params[:format].to_sym […]

Rspec表示,为什么机架式磁盘不能过滤传入的请求

我希望我的Rails 5 API专用应用程序,现在运行在http://localhost:3000 ,只接受来自我的NodeJS前端应用程序的请求,现在运行在http://localhost:8888 。 所以我像这样配置了/config/initializers/cors.rb : Rails.application.config.middleware.insert_before 0, Rack::Cors do allow do origins “http://localhost:8888” resource “*”, headers: :any, methods: [:get, :post, :put, :patch, :delete, :options, :head] end end 我写了这个测试: #/spec/request/cors_request_spec.rb RSpec.feature “CORS protection”, type: :request do it “should accept a request from a whitelisted domain” do get “/api/v1/bodies.json”, nil, “HTTP_ORIGIN”: “http://localhost:8888” expect(response.status).to eql(200) end […]

使用标头渲染JSON

我想在我的控制器中使用以下cors头呈现JSON: ‘Access-Control-Allow-Origin’ = ‘*’. 我试过这个: def my_action render(json: some_params) response.headers[‘Access-Control-Allow-Origin’] = ‘*’ end 但是我得到了一个AbstractController::DoubleRenderError 。 有没有办法用标题呈现JSON?

如何在Rails 4 App中启用CORS

我正要把我的头发拉出来…我从早上开始尝试在这个Rails应用程序中启用CORS,它只是不起作用。 我试过这个 ,使用Rack Cors Gem , 这个答案和这篇文章都没有成功。 有人能指出我正确的方向吗? 这是我的js: var req = new XMLHttpRequest(); if (‘withCredentials’ in req) { // req.open(‘GET’, “https://api.github.com/users/mralexgray/repos”, true); req.open(‘GET’, “http://www.postcoder.lc/postcodes/” + value, true); // Just like regular ol’ XHR req.onreadystatechange = function() { if (req.readyState === 4) { if (req.status >= 200 && req.status < 400) { // JSON.parse(req.responseText) etc. […]