我正在尝试使用 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/