【发布时间】:2017-10-12 10:43:28
【问题描述】:
我已经在本地部署了我的 Play 应用程序,并且在我的浏览器中通过 localhost:9000 访问我的 REST API 运行良好。
但是,从file:///C:/Users/XXX/XXX//index.html 执行 jQuery 脚本(见下文)时出现以下错误:
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:
play.filters.enabled += "play.filters.cors.CORSFilter"
play.filters.cors {
# allow all paths
pathPrefixes = ["/"]
# allow all origins (You can specify if you want)
allowedOrigins = null
allowedHttpMethods = ["GET", "POST", "PUT", "DELETE"]
# allow all headers
allowedHttpHeaders = null
}
注意:我尝试了 allowedOrigins = null 和 allowedOrigins = ["*"]
jQuery 脚本:
$.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));
}
});
Play 是这样说的:
[warn] p.f.c.CORSFilter - Invalid CORS request;Origin=Some(null);Method=GET;Access-Control-Request-Headers=None
【问题讨论】:
-
播放规范说
by default all origins are allowed。您提到您尝试了allowedOrigins = null和allowedOrigins = ["*"]。你试过删除它吗? -
感谢您的提示。不幸的是,它不能解决问题。
-
我认为问题出在
Origin=Some(null)。 Play 会将其验证为isValidOrigin = new URI("null").getScheme ne null。返回错误。您可以尝试使用有效的Origin来执行请求吗?这是Origin = <some valid url>?
标签: playframework playframework-2.6