我想从AngularJS发出一个http get请求。它只能在Internet Explorer 11中运行。当我尝试在Chrome,Firefox或Edge中进行POST调用时,它不起作用。AngularJS - CORS错误只与HTTP GET
我收到以下错误:
这是我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头。
https://stackoverflow.com/questions/1077218/are-different-ports-on-the-same-server-considered-cross-domain-ajax-wise – brso05
它可能与[this](https: //stackoverflow.com/questions/5584923/a-cors-post-request-works-from-plain-javascript-but-why-not-with-jquery)?如果angular正在添加它们,您可能需要在'Access-Control-Allow-Headers'中放置更多的东西。检查请求并确切查看发送了哪些标头。 –
如果端口不同,您违反浏览器的同源策略... – brso05