当前位置 : 主页 > 网页制作 > HTTP/TCP >

playframework – 播放路径配置完全忽略了Http OPTIONS请求

来源:互联网 收集:自由互联 发布时间:2021-06-16
我正在使用Play 2.2.1.我的路由文件中有以下路由配置: OPTIONS /*path controllers.Application.optionsGET / controllers.Application.index...some more routes 我在Applications控制器中进行了以下设置: package cont
我正在使用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

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

用*路径段做一些事情 – 即使没有……

路线:

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
  )
}

有用

网友评论