I'm trying to send api key in header instead of url, but get an error.
With curl everything is okay. I get right json hash:
curl -H "Token: 01PMn25BCj-sluxchSM" http://localhost:3000/api/posts
But Angular gives me the problem:
XMLHttpRequest cannot load http://localhost:3000/api/users/croaton/tags. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3001' is therefore not allowed access. The response had HTTP status code 404.
Rails config
def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Request-Method'] = '*'
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'] = 'X-Requested-With, X-Prototype-Version, Token'
headers['Access-Control-Max-Age'] = '1728000'
render :text => '', :content_type => 'text/plain'
end
end
def authenticate
api_key = request.headers['Token']
@user = User.where(api_key: api_key).first if api_key
unless @user
head status: :unauthorized
return false
end
end
Angular config
RestangularProvider.setDefaultHeaders({'Token': '01PMn25BCj-sluxchSM'});
Getting json
return Restangular.all('posts').getList();