Это мой угловой файл:
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 Search)
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
Наконец, это то, что я разместил в User Controller:
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
@[email protected]
@[email protected]
@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]
@[email protected]
@[email protected]
@verify=Workspace.where('id=? and token=?',@logged_workspace,@logged_token)
unless @verify then
return true
end
return false
end
конец
В результате получается "Нет совпадений для маршрута [ОПЦИИ]"/пользователей. Я читал, что это проблема CORS, и я пробовал все предложения, но все равно это заканчивается. Где я иду не так?
Я использую этот драгоценный камень https://github.com/cyu/rack-cors отлично работает для меня