为什么我需要在我的tomcat应用程序上启用CORS

时间:2015-08-29 01:05:42

标签: java angularjs tomcat cors

我在域上的tomcat 8服务器上运行了一个应用程序 和同一域上的Angular Web应用程序。 Web应用程序使用tomcat应用程序,但是如果没有在web.xml中启用CORS过滤,webapp就会抱怨跨源资源共享,即使它们都在同一个ip上。

以下是我向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.

这对我来说很奇怪,因为如果原点与我所连接的地方相同,那么它不应该是交叉原点。

2 个答案:

答案 0 :(得分:0)

尝试给它一个相对网址而不是规范网址。

这些网站还需要共享同一个端口。 https://en.wikipedia.org/wiki/Same_origin_policy#Origin_determination_rules

另外,请确保获得正确的网址。例如,如果你真的引用了错误"来源' the_IP_of_web_app'",那就失败了,因为它不是一个IP地址。

答案 1 :(得分:0)

交叉源请求策略适用于方案(例如http vs https),域或端口变化的任何时间。因此,在您的情况下,因为端口不同,您将需要CORS。

相关问题