【问题标题】:Http OPTIONS request gets completely ignored by Play route configHttp OPTIONS 请求被 Play 路由配置完全忽略
【发布时间】:2013-12-29 18:46:56
【问题描述】:

我使用的是 Play 2.2.1。我的路由文件中有以下路由配置:

OPTIONS       /*path          controllers.Application.options
GET           /               controllers.Application.index
...some more routes

我在 Applications 控制器中进行了以下设置:

package controllers

import play.api.mvc._

object Application extends Controller {

  def index = Action {
    Ok(views.html.index())
  }

  def options = Action {
    Ok("").withHeaders(
      "Access-Control-Allow-Origin" -> "*",
      "Access-Control-Allow-Methods" -> "GET, POST, PUT, DELETE, OPTIONS",
      "Access-Control-Allow-Headers" -> "Accept, Origin, Content-type, X-Json, X-Prototype-Version, X-Requested-With",
      "Access-Control-Allow-Credentials" -> "true",
      "Access-Control-Max-Age" -> (60 * 60 * 24).toString
    )
  }
}

当我尝试使用 curl 测试 OPTIONS 请求时,它会被游戏完全忽略。

curl -X OPTIONS --include 'http://localhost:9000/foo/139'

我得到了这个错误:

HTTP/1.1 404 Not Found
Content-Type: text/html; charset=utf-8
Content-Length: 7045



<!DOCTYPE html>
<html>
    <head>
        <title>Action not found</title>

...some more head junk

    <body>
        <h1>Action not found</h1>

        <p id="detail">
            For request 'OPTIONS /foo/139'
        </p>



                <h2>
                    These routes have been tried, in this order:
                </h2>

                <div>

            <pre><span class="line">1</span><span class="route"><span class="verb">GET</span><span class="path">/</span><span class="call">controllers.Application.index</span></span></pre>

... more routes but none of them are for the OPTIONS request            

我在这里做错了什么?提前致谢!

【问题讨论】:

    标签: playframework playframework-2.0


    【解决方案1】:

    对于任何对此 Java Equivalent 感兴趣的人,它是:

    在你的 conf\routes 中:

    OPTIONS    /          controllers.OptionsController.options(path: String ?= "")
    OPTIONS    /*path     controllers.OptionsController.options(path)
    

    在 app\controllers 中,文件 OptionsController.java:

    package controllers;
    
    import play.mvc.Controller;
    import play.mvc.Result;
    
    public class OptionsController extends Controller {
    
        public Result options(String path) {
    
            return ok("").withHeaders(
                      "Access-Control-Allow-Origin" , "*",
                      "Access-Control-Allow-Methods" , "GET, POST, PUT, DELETE, OPTIONS",
                      "Access-Control-Allow-Headers" , "Accept, Origin, Content-type, X-Json, X-Prototype-Version, X-Requested-With",
                      "Access-Control-Allow-Credentials" , "true",
                      "Access-Control-Max-Age" , Integer.toString(60 * 60 * 24)
            );
        }
    }
    

    【讨论】:

      【解决方案2】:

      在你的 application.conf 中添加这一行:

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

      您不需要设置任何其他标题等。

      【讨论】:

        【解决方案3】:

        *path 段做点什么——即使什么都没有......

        路线:

        OPTIONS    /          controllers.Application.options(path: String ?= "")
        OPTIONS    /*path     controllers.Application.options(path)
        

        行动:

        def options(path: String) = Action {
          Ok("").withHeaders(
            "Access-Control-Allow-Origin" -> "*",
            "Access-Control-Allow-Methods" -> "GET, POST, PUT, DELETE, OPTIONS",
            "Access-Control-Allow-Headers" -> "Accept, Origin, Content-type, X-Json, X-Prototype-Version, X-Requested-With",
            "Access-Control-Allow-Credentials" -> "true",
            "Access-Control-Max-Age" -> (60 * 60 * 24).toString
          )
        }
        

        有效

        【讨论】:

        • 感谢您的回复。我添加了更改,但仍然不好。令我震惊的是,在尝试过的路由列表中(在 404 响应中)甚至没有列出带有 OPTION 动词的路由!
        • 重新部署可能有问题?尝试清理应用并再次运行
        • 这实际上是问题所在。由于某种原因,路由文件没有正确重新编译。即使我退出正在运行的播放应用程序并手动运行“编译”。我不得不做一个“彻底玩”然后重新开始玩。这是一个错误还是我做错了什么?
        • 有时会发生奇怪的事情,即。在工作开发应用程序期间更改 git 分支时,无论如何这是相当罕见的情况
        猜你喜欢
        • 1970-01-01
        • 2011-01-16
        • 1970-01-01
        • 1970-01-01
        • 2012-11-19
        • 1970-01-01
        • 2019-09-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多