gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:39:28 24 4
gpt4 key购买 nike

我使用的是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com