【问题标题】:what is wrong with my CORS configuration ?我的 CORS 配置有什么问题?
【发布时间】:2014-04-11 01:21:55
【问题描述】:

给定一些客户端 js

(function(){
  var submitEmail = function () {
    $("#join").click(function(e){
      var email = $("#email").val()
      if ( email.length === 0) { return false}
      console.log(email)
      $.ajax({
        crossDomain: true,
        type: "POST",
        url: "https://www.rubyonrailstutor.com/join.json",
        data: {first_name: "hacker", last_name: "github.io", email: "email" },
        dataType: "JSON"
      })
    });
  }
  submitEmail()

给定 Rails 应用程序控制器中的以下配置

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :set_headers

  private

  def set_headers
    headers['Access-Control-Allow-Origin'] = 'http://rubyonrailstutor.github.io'
    headers['Access-Control-Allow-Methods'] = 'POST'
    headers['Access-Control-Allow-Headers'] = '*,x-requested-with,Content-Type,If-Modified-Since,If-None-Match,Auth-User-Token'
    headers['Access-Control-Expose-Headers'] = 'ETag'
  end
end

在heroku上获得以下行为

2014-03-07T18:17:34.258456+00:00 app[web.1]: Started POST "/join.json" for 99.108.137.170 at 2014-03-07 18:17:34 +0000
2014-03-07T18:17:34.363521+00:00 heroku[router]: at=info method=POST path=/join.json host=www.rubyonrailstutor.com request_id=96ce40a0-652a-4d40-ab29-a364e47d4815 fwd="99.108.137.170" dyno=web.1 connect=3ms service=113ms status=200 bytes=897
2014-03-07T18:18:14.482579+00:00 app[web.1]: Started POST "/join.json" for 99.108.137.170 at 2014-03-07 18:18:14 +0000
2014-03-07T18:18:14.491328+00:00 heroku[router]: at=info method=POST path=/join.json host=www.rubyonrailstutor.com request_id=cdb3e5dd-b200-4aa4-9fd2-c97688f64f58 fwd="99.108.137.170" dyno=web.1 connect=2ms service=12ms status=200 bytes=897
2014-03-07T18:20:38.956777+00:00 app[web.1]: Started POST "/join.json" for 99.108.137.170 at 2014-03-07 18:20:38 +0000

似乎没有足够的数据传递给服务器,即,有数据参数并且服务器似乎没有处理任何东西,正在发出请求,然后我不知道发生了什么,我应该研究什么来解决这个问题?

谢谢!

【问题讨论】:

    标签: ruby-on-rails cors


    【解决方案1】:

    您应该使用 rack-cors gem,它可以让您轻松做到这一点。

    Rack::Cors 为 Rack 兼容的 Web 应用程序提供跨域资源共享 (CORS) 支持。 CORS 规范允许 Web 应用程序进行跨域 AJAX 调用

    Gemfile.rb

    gem 'rack-cors',
      :require => 'rack/cors'
    

    config/application.rb

    module Sample
      class Application < Rails::Application
        # other application config
    
        config.middleware.use Rack::Cors do
          allow do
            origins 'https://www.rubyonrailstutor.com/'
            resource %r{/users/\d+.json},
              :headers => ['Origin', 'Accept', 'Content-Type'],
              :methods => [:put, :delete]
          end
        end
      end
    end
    

    你可以找到更详细的使用here

    【讨论】:

    • 似乎 stackoverflow.com/questions/19939207/… 是对这个 q 的一个很好的跟进
    • 当然,更重要的是你绕过你的问题。
    • 之间,我刚刚检查了链接,我的答案仍然是正确的。你知道stackoverflow就是这样工作的,你肯定可以找到更详细的答案,但这不是撤消验证的理由。
    • 啊,是的,对不起,那是我的错。另一个答案很好,我非常感谢你的帖子。
    猜你喜欢
    • 2017-03-08
    • 2014-01-13
    • 2011-09-11
    • 2013-07-17
    • 1970-01-01
    • 2012-11-06
    • 2014-05-31
    • 1970-01-01
    • 2011-09-03
    相关资源
    最近更新 更多