$ http.post成为Rails路由中的[OPTIONS]

时间:2015-08-18 08:20:52

标签: ruby-on-rails angularjs api post cors

这是我的Angular文件:

registrationModule.config(['$httpProvider', function($httpProvider) {
        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common['X-Requested-With'];
    }
]);

registrationModule.controller("registrationController",function($scope,$http){
        $http.get("http://localhost:3000/logins/workspace_list?token=tempore").
        success(function(response) {
            $scope.workspaces=response;
        });

        $scope.register=function(){
            var postData=$scope.module_entry;

            if(postData.password==postData.repassword){
                $http.post('http://localhost:3000/users',postData);
            }
            else {
                alert("Password not correctly verified");


            }
        }

});

我把它放在我的Rails应用程序控制器中(我是API的新手,所以我正在从我在Google搜索中找到的内容中琢磨我所知道的)

  class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_filter :cors_preflight_check
  after_filter :cors_set_access_control_headers

  def cors_set_access_control_headers
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
    headers['Access-Control-Allow-Headers'] = 'Origin, Content-Type, Accept, Authorization, Token'
    headers['Access-Control-Max-Age'] = "1728000"
  end

  def cors_preflight_check
    if request.method == 'OPTIONS'
      headers['Access-Control-Allow-Origin'] = '*'
      headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
      headers['Access-Control-Allow-Headers'] = 'Content-Type,X-Requested-With, X-Prototype-Version, Token'
      headers['Access-Control-Max-Age'] = '1728000'

      render :text => '', :content_type => 'text/plain'
    end
  end
end

最后这就是我放在用户控制器中的内容:

class UsersController < ApplicationController

def index

end

def show
    @user=User.where('username=? and password=?',params[:username],params[:password])
    unless(!@user) do
        return @user        

    end

    render nothing, :status=>400


end 

def create

    unless(!authenticate()) then
        @user=User.new
        @user.username=@registration.username
        @user.password=@registration.password
        @user.token=(0...8).map { (65 + rand(26)).chr }.join
        @user.workspace=Workspace.find(@registration.workspace)
        @user.save

        render json: @user

    end


    render :html, :status=>400


end

def authenticate

    @registration=JSON.parse params[:module_entry]

    @logged_workspace=@registration.workspace
    @logged_token=@registration.token

    @verify=Workspace.where('id=? and token=?',@logged_workspace,@logged_token)

    unless @verify then
        return true
    end

    return false



end

最终会出现“路线[OPTIONS]”/用户无匹配。我读到这是一个CORS问题,我已经尝试了所有的建议,但它仍然像这样结束。我哪里错了?

1 个答案:

答案 0 :(得分:0)

我正在使用这个宝石https://github.com/cyu/rack-cors对我来说很好