简体   繁体   中英

Implementing CORS in Play Framework 2.4 for Scala

I am not yet succeeding in making CORS requests work in my project, basically a Play server and an AngularJS web client.

Since there are many variables to take into consideration, I would like first to be sure that everything is ok at least on the Play server side. But, my fault, the Play 2.4 documentation on CORS is not enough to help me figure out a concrete use case.

Two basic questions:

  1. Creating a Filters class in the root of your project (see docs ): where to place the provided code? Not sure what the "root of your project" is, I added the class to my controller in app/controllers/Mycontr.scala
  2. The points described in the documentation are enough? Or other parts need to be touched as well, for example routing in app/conf/routes

Tried to find a complete tutorial, but had no luck for Play 2.4

"root of your project" means a directory which corresponds to the package root. It is app/ by default, src/main/scala/ when using default sbt layout . That's all.

I am running into the same, issue, it keeps saying:

XMLHttpRequest cannot load http://localhost:9000/fetchBetween . Response to preflight request doesn't pass access control check: No 'Access-Control Allow-Origin' header is present on the requested resource. Origin ' http://localhost:3000 ' is therefore not allowed access. The response had HTTP status code 404.

I added Filters.scala to my app/ folder, and added filters to libraryDependencies , this is my config in application.conf :

play.filters.cors {

  pathPrefixes = ["/fetchBetween"]

  allowedOrigins = ["*"]

  allowedHttpMethods = ["GET", "POST"]

  allowedHttpHeaders = ["Accept"]

  preflightMaxAge = 3 days

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM