Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
播放框架AngularJs CORS post json_Json_Scala_Post_Playframework 2.0_Cors - Fatal编程技术网

播放框架AngularJs CORS post json

播放框架AngularJs CORS post json,json,scala,post,playframework-2.0,cors,Json,Scala,Post,Playframework 2.0,Cors,我有一个AngularJs前端和一个playframework 2 Scala后端。查看如何将CORs添加到后端-我有两个端点:GET和POST GET /api/question/:id controllers.Questions.getQuestion(id: String) POST /api/question/:id controllers.Questions.postQuestion(id: String

我有一个AngularJs前端和一个playframework 2 Scala后端。查看如何将CORs添加到后端-我有两个端点:GET和POST

GET         /api/question/:id              controllers.Questions.getQuestion(id: String)
POST        /api/question/:id              controllers.Questions.postQuestion(id: String)
其他响应中提供的此解决方案适用于GET,但不适用于POST

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

  }




def postQuestion(key:String) = CorsAction(parse.json){
               req =>
      ........
  }
我所理解的是,扩展ActionBuilder的CORsAction也可以接受bodyparser所使用的方法,因此也应该基本上适用于POST请求

这是我从前端点击POST时在浏览器控制台中看到的

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. 

因此,我不确定确切的问题,但添加以下路线 解决了这个问题

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

其中,飞行前endpont仅返回带有CORs标头的Ok。但我原以为议会应该负责这件事。无论如何,继续。

您如何返回CORS标题?使用Ok.withHeaders…?对于每个Ok…,是否添加withHeadersACCESS\u CONTROL\u ALLOW\u ORIGIN->*?我可以通过在我的每个HTTP响应头中添加WithHeader来解决我的CORS问题,而不仅仅是飞行前响应的头。我只需要将它添加到我的选项请求中。如果这是你的意思的话,不适用于GET/POST。嗯,只有在添加WithHeader之后。。。对于我的GET/POST请求,CORS也为我工作。