playframework - Play 路由配置完全忽略 Http OPTIONS 请求

标签 playframework playframework-2.0

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

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

我在应用程序 Controller 中进行了以下设置:

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 请求时,它完全被 Play 忽略。

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            

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

最佳答案

使用 *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
  )
}

有效

关于playframework - Play 路由配置完全忽略 Http OPTIONS 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20525178/

相关文章:

scala - 在 Scala Play 框架中是否有一种快速内置的方式来转发请求

java - 有没有成熟的基于Cookie的HttpSession实现?

java - 如何异步接受 WebSocket?

java - Play framework 2.5.x Web Socket Java

scala - Play 2.5 替换 Trait 中的 current.injector

scala - 从 Play 2.0 Scala Controller 中的请求获取表单参数值

playframework - 如何让 Playframework 在不发出请求的情况下开始在开发模式下运行应用程序?

java - 如何在 Play Framework 中进行详细编译?

java - 如何为 Java 库中没有应用方法的对象编写 JSON 格式?

scala - Play 2 : How to test controllers with non-empty request body