Pregunta

I have a frontend in AngularJs and a playframework 2 Scala backend. Looking at this to add CORs to the backend - I have two endpoints a GET and POST

GET         /api/question/:id              controllers.Questions.getQuestion(id: String)
POST        /api/question/:id              controllers.Questions.postQuestion(id: String)

This solution provided in the other response works for the GET but not for the POST

def getQuestion(key:String) = CorsAction{
    ....

  }




def postQuestion(key:String) = CorsAction(parse.json){
               req =>
      ........
  }

What I understood is that the CORsAction (which extends ActionBuilder) can also accept methods which a bodyparser and thus should essentially work for POST requests as well.

This is what I see in the browser console when i hit POST from my front end.

OPTIONS http://localhost:9000/api/question/abc123 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3001' is therefore not allowed access. angular.js:7997
XMLHttpRequest cannot load http://localhost:9000/api/question/abc123. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3001' is therefore not allowed access. 
¿Fue útil?

Solución

So I'm not sure the exact problem but adding the following to the routes fixed the problem

OPTIONS        /api/question/:id             controllers.Application.preflight(id)

Where the preflight endpont just returns Ok with the CORs headers. But I would have thought that the CorsAction was supposed to take care of this. Anyways, moving on.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top