gpt4 book ai didi

javascript - 如何在 play-framework 2.5.x 中实现跨源资源共享 (CORS)

转载 作者:搜寻专家 更新时间:2023-11-01 05:04:02 24 4
gpt4 key购买 nike

我正在尝试使用 Angularjs-1 应用程序通过从本地主机访问 Restful URL 来获取 json 数据

我收到这个错误

http://localhost:9000/mlm/user/all Failed to load resource: 
the server responded with a status of 404 (Not Found)

index.html:1 XMLHttpRequest cannot load http://localhost:9000/mlm/user/all.

Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource.

Origin 'http://localhost:63342' is therefore not allowed access.

The response had HTTP status code 404.

我正在使用 play-framework 2.5.4 (java)。

编辑 1:将 CORS 设置添加到 app.conf

    play.filters {
cors {
# Filter paths by a whitelist of path prefixes
pathPrefixes = ["/"]

# The allowed origins. If null, all origins are allowed.
allowedOrigins = null

# The allowed HTTP methods. If null, all methods are allowed
allowedHttpMethods = ["GET", "POST"]

allowedHttpHeaders = ["Accept"]
preflightMaxAge = 3 days
}
}

最佳答案

最后这对我有用

根据 official文档

Filter.java 是(没有工作):

import play.mvc.EssentialFilter;
import play.filters.cors.CORSFilter;
import play.http.DefaultHttpFilters;

import javax.inject.Inject;

public class Filters extends DefaultHttpFilters {
@Inject public Filters(CORSFilter corsFilter) {
super(corsFilter);
}
}

但它确实奏效了。有效的是:

Filter.java(有效)

import play.mvc.EssentialFilter;
import play.filters.cors.CORSFilter;
import play.http.HttpFilters;
import javax.inject.Inject;

public class Filters implements HttpFilters {

@Inject
CORSFilter corsFilter;

public EssentialFilter[] filters() {
return new EssentialFilter[] { corsFilter.asJava() };
}
}

感谢this回答,关于堆栈溢出的类似问题。

但是为什么官方的Filter.java代码docs 2.5.x 不工作是百万美元的问题?

关于javascript - 如何在 play-framework 2.5.x 中实现跨源资源共享 (CORS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38370308/

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