I have a java play framework 2.4.x web app providing a JSON/HTTP API. When I run my front-end HTML/JS file:///Users/nize/tmp/index.html
calling the API on http://localhost:9000
chrome shows
XMLHttpRequest cannot load http://localhost:9000.
No 'Access-Control-Allow-Origin' header is present
on the requested resource. Origin 'null' is therefore
not allowed access. The response had HTTP status code 403.
I have configured the web app as per the instructions given in Play Framework 2.4.x CORS Documentation:
- Update to
build.sbt
- Added the class
Filters.java
to the root of the project (also tried/app
) Added the following stanza to the
application.conf
play.filters.cors { allowedOrigins = ["*","http://localhost"] #allowedHttpMethods = ["GET", "POST"] #allowedHttpHeaders = ["Accept"] #preflightMaxAge = 3 days }
What am I missing?
Edit: The symptoms look identical or similar to Other very similar stackoverflow post. That problem was solved by reconfiguring Cisco AnyConnect VPN which was installed on the computer. I, however, don't have that software installed.
I think the CORS filter in Play does not work! I followed step by step as but somehow I always got HTTP-403 in the browser (Chrome and Firefox) in Ajax calls. Problem is I don't even get stacktrace on server side. I think
DefaultHttpErrorHandler
in the CORS filter somehow gulp that. In the response "Access-Control-Allow-Origin" header was missing so I just manually added that.This is the logging filter (Credit: Play! framework)
This will probably not solve the problem for the poster, but it solved the problem for me when I had the same symptoms, and I am posting it in case it can help others in the same situation.
I had actually misunderstood how CORS is working. I have two separate Play applications, one with a REST API and one with a web interface using the REST API. I followed the instructions in the documentation page mentioned in the question, but my mistake was that I did it on the web interface application. When I instead did it on the REST API application, it worked immediately.
I had the same problem while following the same documentation.
Problem is with this CORS filter that you have used:
If you want to allow all origins use:
Follow the same for
allowedHttpMethods
This is as per the documentation
To quote:
Hope this helps!
Does it work in Firefox? Chrome has some restrictions on running Ajax from local files, e.g., see this link. If it is Chrome-specific, you can start up Chrome with a command-line switch to remove the restriction.
I was experiencing a similar issue, I was getting 403's on requests. I solved a the problem by removing the:
that they use in their example configuration. I'm still not clear what the security implications of that are, however, so YMMV.