简体   繁体   English

AngularJS-仅使用HTTP GET的CORS错误

[英]AngularJS - CORS Error only with HTTP GET

I am trying to make a http get request from AngularJS. 我正在尝试从AngularJS发出http获取请求。 It is only working in the Internet Explorer 11. When I try to make a POST Call in Chrome, Firefox or Edge it is not working. 它仅在Internet Explorer 11中工作。当我尝试在Chrome,Firefox或Edge中进行POST呼叫时,它无法工作。

I get the following error: 我收到以下错误:

CORS Error in Chrome Chrome中的CORS错误

This is my AngularJS Call: 这是我的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);
              });

As backend I have a Java Spring MVC Application and I have set the following headers: 作为后端,我有一个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");

But I saw that angular never reaches the backend when I set a breakpoint! 但是我看到在设置断点时,角度永远不会到达后端! This is very confusing, I already searched a lot in the internet and tried to changed the default AngularJS Content-Type header: 这非常令人困惑,我已经在互联网上进行了大量搜索,并尝试更改默认的AngularJS Content-Type标头:

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

I tried also to change the Content-Type in the POST-Request to "application/x-www-form-urlencoded" - with no effect. 我还尝试将POST请求中的Content-Type更改为“ application / x-www-form-urlencoded”-无效。

Note: The backend and the frontend runs on the same dev machine. 注意:后端和前端在同一台开发机上运行。 The only difference is the port, that's why I am confused that I get a CORS Error at all... 唯一的区别是端口,这就是为什么我很困惑我完全收到CORS错误的原因...

I have this problem only with POST-Requests; 我只有POST请求有这个问题; GET works like a Charm GET就像魅力一样

Edit 1: 编辑1:

I thought that this is a Problem with cors and that i use different ports, but is it possible that I can turn that off? 我以为这是cors的问题,我使用了不同的端口,但是我有可能将其关闭吗?

Edit 2: 编辑2:

Now i try to get it to work with a local tomcat. 现在,我尝试使其与本地tomcat一起使用。 Now i see in the Tomcat the request will reach the server: 现在我看到在Tomcat中,请求将到达服务器:

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

The Response is now HTTP 403. In Browsers network tab i can see the following request header: 现在,响应为HTTP403。在“浏览器”网络标签中,我可以看到以下请求标头:

  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

The Response of that request header is the following: 该请求标头的响应如下:

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

I have set the cors filter like the example in the tomcat documentation: http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter 我已经像tomcat文档中的示例一样设置了cors过滤器: http : //tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter

When i make a get request, it is still working and all CORS headers are present. 当我发出get请求时,它仍在工作,并且所有CORS标头都存在。 I think there is something wrong with the first OPTIONS request. 我认为第一个OPTIONS请求有问题。 I dont know why the tomcat do not set the CORS header when the OPTIONS request comes. 我不知道为什么当OPTIONS请求到来时,tomcat没有设置CORS标头。

In your Spring Controller, add @CrossOrigin('http://localhost:8080') 在您的Spring Controller中,添加@CrossOrigin('http://localhost:8080')

annotation. 注解。 Of course, substitute out the port of your angular application. 当然,请替换您的角度应用程序的端口。

Since you are getting 400 (not authorized) error, the request is not reaching the service itself. 由于出现400(未经授权)错误,因此请求无法到达服务本身。 Most probably the request domain is blocked, you may see if XHR requests are allowed and check cross origin policy if needed. 最有可能是请求域被阻止,您可能会看到是否允许XHR请求,并在需要时检查跨源策略。

I figured it out. 我想到了。 I made a completly new angularjs testwebsite. 我创建了一个全新的angularjs测试网站。 This is my working angularjs Code: 这是我工作的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);
  });
}]);

I deleted anything that changed the default angularjs headers. 我删除了所有更改默认angularjs标头的内容。 The headers of my Spring MVC Application are the following: 我的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");

I dont know the exactly cause why it is working now. 我不知道它现在为什么起作用的确切原因。 Maybe it was because i modified the default headers in AngularJS. 也许是因为我修改了AngularJS中的默认标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM