Java Play Framework 2.6不返回`Access Control Allow Origin`CORS标头
Play不支持我的Java Play Framework 2.6不返回`Access Control Allow Origin`CORS标头,java,playframework,cors,playback,Java,Playframework,Cors,Playback,Play不支持我的application.conf配置以返回访问控制允许源站标题。当我从Ajax发送请求时,响应总是: 加载失败http://localhost:9000/: 请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源站“null”。 这就是我的application.conf看起来的样子: # disable the built in filters play.http.filters = play.api.http.NoHttpFil
application.conf
配置以返回访问控制允许源站
标题。当我从Ajax发送请求时,响应总是:
加载失败http://localhost:9000/: 请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源站“null”。
这就是我的application.conf
看起来的样子:
# disable the built in filters
play.http.filters = play.api.http.NoHttpFilters
play.filters.enabled += "play.filters.cors.CORSFilter"
play.filters {
cors {
# The allowed origins. If null, all origins are allowed.
allowedOrigins = null
}
}
我成功地通过Ajax请求的唯一方法是添加行response().setHeader(“访问控制允许源代码“,”*”)控制器中的代码>。但是我想通过application.conf
来完成,所以我不必在所有控制器中添加这个setHeader()
代码
public class HomeController extends Controller {
public Result index() {
// this is the only hack that works
//response().setHeader("Access-Control-Allow-Origin", "*");
return ok(
Json.toJson("A long time ago in a galaxy far, far away....")
);
}
}
我的Ajax请求如下所示:
$.ajax({
url: "http://localhost:9000",
type: "GET",
dataType: 'json',
success: function(_out) {
alert(_out);
},
fail: function() {
alert("error");
}
});
为什么Play不尊重我的application.conf
CORS配置?我已经确定了问题所在。在我的application.conf
中:
play.http.filters = play.api.http.NoHttpFilters
play.filters.enabled += "play.filters.cors.CORSFilter"
这两条线不是相加的。换句话说,第一行禁用所有过滤器,第二行启用过滤器,但最终结果仍然是禁用所有过滤器(即使禁用之后才启用)