2017-06-27 54 views
0

我想从AngularJS发出一个http get请求。它只能在Internet Explorer 11中运行。当我尝试在Chrome,Firefox或Edge中进行POST调用时,它不起作用。AngularJS - CORS错误只与HTTP GET

我收到以下错误:

CORS Error in Chrome

这是我AngularJS电话:

$http({ 
       method: 'POST', 
       headers: { 
       'Content-Type': 'application/json' 
       }, 
       url: URL 
       }).then(function successCallback(response) { 
        console.log(response.data); 

       }, function errorCallback(response) { 
       console.log(response.status); 
       console.log(response.headers); 
       console.log(response.config); 
       }); 

作为后端我有一个Java Spring MVC的应用程序,我有设置以下标题:

HttpHeaders responseHeader = new HttpHeaders(); 
    responseHeader.set("Access-Control-Allow-Origin","*"); 
    responseHeader.set("Access-Control-Allow-Headers:","Content-Type"); 
    responseHeader.set("Access-Control-Allow-Methods","POST, GET, PUT, OPTIONS, DELETE"); 
    responseHeader.set("Content-Type","application/json"); 

但是我看到角度从未到达bac肯定当我设置断点!这是非常混乱,我已经搜查了很多在互联网上,并试图更改默认AngularJS Content-Type头:

$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'; 

我试着也改变内容类型的POST请求为“application/x-www-form-urlencoded“ - 没有效果。

注意:后端和前端运行在同一个开发机器上。唯一的区别是端口,这就是为什么我很困惑,我得到一个CORS错误...

我只有POST请求这个问题;得到这样一个魅力

编辑1作品:

我认为这是CORS和一个问题,我使用不同的端口,但有可能,我可以把它们关掉?

编辑2:

现在我设法得到它与一个本地的Tomcat工作。现在我在Tomcat看到该请求将到达服务器:

<< Client IP>> - - [28/Jun/2017:13:43:24 +0200] "OPTIONS <<URL>> HTTP/1.1" 403 - 

的响应现在是HTTP 403在浏览器中的网络标签我可以看到下面的请求报头:

Host: <<backend ip>>:8080 
User-Agent: <<user agent>> 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: de,en-US;q=0.7,en;q=0.3 
Accept-Encoding: gzip, deflate 
Access-Control-Request-Method: POST 
Access-Control-Request-Headers: access-control-allow-origin,content-type 
Origin: http://<<Ip of host>> 
Connection: keep-alive 

的,响应请求首标是如下:

Content-Type: text/plain 
Content-Length: 0 
Date: Wed, 28 Jun 2017 11:43:24 GMT 

我已经设置了CORS过滤等Tomcat文档中的示例:http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter

当我做出请求时,它仍然可以工作,并且所有的CORS头文件都存在。 我认为第一个OPTIONS请求有问题。我不知道为什么tomcat在OPTIONS请求到来时不设置CORS头。

+1

https://stackoverflow.com/questions/1077218/are-different-ports-on-the-same-server-considered-cross-domain-ajax-wise – brso05

+0

它可能与[this](https: //stackoverflow.com/questions/5584923/a-cors-post-request-works-from-plain-javascript-but-why-not-with-jquery)?如果angular正在添加它们,您可能需要在'Access-Control-Allow-Headers'中放置更多的东西。检查请求并确切查看发送了哪些标头。 –

+0

如果端口不同,您违反浏览器的同源策略... – brso05

回答

0

我想通了。我做了一个全新的angularjs测试网站。这是我的工作angularjs代码:

var req = { 
method: 'POST', 
url: 'myURL', 
headers: { 
    'Content-Type': undefined 
}, 
data: "JSON BODY DATA" 
} 

$http(req).then(function successCallback(response) { 
    console.log(response.data); 
    }, function errorCallback(response) { 
    console.log(response.data); 
    }); 
}]); 

我删除了任何更改默认angularjs标头。 我的Spring MVC应用程序的标题如下:

HttpHeaders responseHeader = new HttpHeaders(); 
responseHeader.set("Access-Control-Allow-Origin","http://<<clientip>>"); 
responseHeader.set("Access-Control-Allow-Headers:","Content-Type"); 
responseHeader.set("Access-Control-Allow-Methods","POST, GET, PUT, OPTIONS, DELETE"); 
responseHeader.set("Content-Type","application/json"); 

我不知道确切的原因造成它现在的工作。也许是因为我修改了AngularJS中的默认标题。

0

在Spring控制器,添加@CrossOrigin('http://localhost:8080')

注解。当然,替换你的角度应用程序的端口。

+0

它不工作... – HannesR

0

由于您收到400(未授权)错误,请求未达到服务本身。很可能请求域被阻止,您可能会看到是否允许XHR请求,并在需要时检查跨源策略。