Juliusz
Juliusz

Reputation: 1370

Play Framework CORS request

I have two apps:

  1. Play 2.6.7 app running at localhost:9000
  2. webpack dev server that runs at localhost:3000

The webpack app makes a POST request to the Play app

$.ajax({
  url: 'http://localhost:9000/users',
  data: JSON.stringify(data),
  dataType: 'json',
  method: 'POST'
})

To which the Play app responds with

Failed to load http://localhost:9000/users: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

Only when I explicitly set the header in the Play app with

def create = Action { 
  Ok("stuff").withHeaders(
    "Access-Control-Allow-Origin" -> "http://localhost:3000"
  )
}

does the request go through without error.

My question is: why doesn't Play set this header automatically as the docs seem to suggest? My application.conf is an empty file.

Upvotes: 5

Views: 5771

Answers (1)

Juliusz
Juliusz

Reputation: 1370

The CORS filter isn't enabled by default. Adding

play.filters.enabled += play.filters.cors.CORSFilter

to application.conf solves this issue

Upvotes: 9

Related Questions