Playアプリをローカルに配置し、ブラウザ内からlocalhost:9000
経由でREST APIにアクセスしても問題ありません。 jQueryのスクリプトを実行するときにPlay CORSが動作しない
はしかし、私はfile:///C:/Users/XXX/XXX//index.html
から(下記参照)、次のエラーを取得:
Failed to load resource: the server responded with a status of 403 (Forbidden)
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403.
私はhttps://www.playframework.com/documentation/2.6.x/CorsFilterの下に与えられた指示に従いました。
マイbuild.sbt
:
libraryDependencies ++= Seq(
jdbc,
evolutions,
guice,
"com.h2database" % "h2" % "1.4.194",
javaJpa,
"org.hibernate" % "hibernate-core" % "5.2.5.Final",
filters
)
マイapplication.conf
:
注:
$.ajax({
'url' : 'http://localhost:9000/employee/' + user_id + '/weeklyWorkingHours',
'type' : 'GET',
'success' : function(data) {
console.log('Data: '+ JSON.stringify(data));
},
'error' : function(request,error)
{
console.log("Error: "+JSON.stringify(request));
}
});
:私は両方
allowedOrigins = null
&
allowedOrigins = ["*"]
jQueryのスクリプトを試してみましたここ
言うプレイです:
[warn] p.f.c.CORSFilter - Invalid CORS request;Origin=Some(null);Method=GET;Access-Control-Request-Headers=None
プレイ仕様として、あなたのファイルを読み込むことができ
で、たとえば、ローカルWebサーバーを設定することができます 'デフォルトでは、すべての起源はallowed'であることを述べています。あなたは 'allowedOrigins = null'と' allowedOrigins = ["*"] 'の両方を試みたと言います。あなたはそれを削除しようとしましたか? – Bruno
ヒントをありがとう。残念ながら、それはトリックをしません。 – mollwitz
私はこの問題が 'Origin = Some(null)'にあると思います。 Playはこれを 'isValidOrigin =新しいURI(" null ")として検証します。getScheme ne null'。それは偽を返します。あなたは有効な 'Origin'を使ってリクエストを行うことができますか?これは 'Origin ='ですか? –
Bruno