Getting started with PhoneGap, using yeoman, bower and grunt setup with Angular. I'm trying to get a response from my Rails API and always get XMLHttpRequest cannot load http://localhost:3000/api/v1/sessions. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.
I've tried what feels like every solution in the book, but can't get this working.
Here's the necessary code:
class Api::V1::BaseController < ActionController::Base
respond_to :json
protect_from_forgery with: :null_session
before_filter :set_cors_headers
before_filter :cors_preflight
def set_cors_headers
headers['Access-Control-Allow-Origin'] = AppConfig.client['origin']
headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS'
headers['Access-Control-Allow-Headers'] = '*'
headers['Access-Control-Max-Age'] = "3628800"
end
def cors_preflight
head(:ok) if request.method == :options
end
end
In my app_config.yml file:
defaults: &defaults
client:
origin: http://localhost:9000
development:
<<: *defaults
test:
<<: *defaults
production:
<<: *defaults
I'm loading this in an initializer, app_config.rb:
require 'ostruct'
require 'yaml'
config = YAML.load_file(File.join(Rails.root, 'config', 'app_config.yml')) || {}
AppConfig = OpenStruct.new(config[Rails.env] || {})
I tried using the Rack CORS gem as well, and have this in my application.rb:
module MyApp
class Application < Rails::Application
config.middleware.use Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options, :delete]
end
end
end
end
Using Rails 4.1.