Play не учитывает мою application.conf
конфигурацию для возврата заголовка Access-Control-Allow-Origin
. Когда я отправляю запрос от Ajax, ответ всегда:
Failed to load http://localhost:9000/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
Вот как выглядит мой 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("Access-Control-Allow-Origin", "*");
в контроллер. Но я хочу сделать это через 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?