为什么我需要在 tomcat 应用程序上启用 CORS
Why do I need to enable CORS on the tomcat application
我在域中的 tomcat 8 服务器上有一个应用程序 运行
和一个 Angular 网络应用程序。 Web 应用程序使用 tomcat 应用程序,但如果不在 web.xml 中启用过滤,Web 应用程序将出现问题。
这是我向 tomcat 应用程序发出 js 请求的方式
function getResources($http, url){
return $http({
method: 'GET',
url: url,
headers: {'Accept': 'application/json'}
}).then(function (result) {
return result.data;
});
};
我也尝试过:dataType: 'jsonp'
错误看起来像:
XMLHttpRequest cannot load http://localhost:8080/APP/requestStuff.No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.
这对我来说很奇怪,因为如果原点与我连接的地方相同,那不应该是交叉原点。
尝试给它一个相对的 url 而不是规范的。
站点还需要共享同一个端口。 https://en.wikipedia.org/wiki/Same_origin_policy#Origin_determination_rules
此外,请确保您的选择正确 URL。例如,如果您按字面意思将错误引用为 "Origin 'the_IP_of_web_app'",则失败是因为那不是 IP 地址。
只要方案(例如 http 与 https)、域或端口不同,跨源请求策略就会适用。因此,在您的情况下,因为端口不同,您将需要 CORS。
我在域中的 tomcat 8 服务器上有一个应用程序 运行 和一个 Angular 网络应用程序。 Web 应用程序使用 tomcat 应用程序,但如果不在 web.xml 中启用过滤,Web 应用程序将出现问题。
这是我向 tomcat 应用程序发出 js 请求的方式
function getResources($http, url){
return $http({
method: 'GET',
url: url,
headers: {'Accept': 'application/json'}
}).then(function (result) {
return result.data;
});
};
我也尝试过:dataType: 'jsonp'
错误看起来像:
XMLHttpRequest cannot load http://localhost:8080/APP/requestStuff.No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.
这对我来说很奇怪,因为如果原点与我连接的地方相同,那不应该是交叉原点。
尝试给它一个相对的 url 而不是规范的。
站点还需要共享同一个端口。 https://en.wikipedia.org/wiki/Same_origin_policy#Origin_determination_rules
此外,请确保您的选择正确 URL。例如,如果您按字面意思将错误引用为 "Origin 'the_IP_of_web_app'",则失败是因为那不是 IP 地址。
只要方案(例如 http 与 https)、域或端口不同,跨源请求策略就会适用。因此,在您的情况下,因为端口不同,您将需要 CORS。