Angular -js cors request not reach server

I have a spring MVC API backend with the correct CORS setting when I try to make an ajax call, I get the following error in chrome

XMLHttpRequest cannot load 172.20.16.45:8082/cuponza. The request was redirected to "172.20.16.45:8082/cuponza/", which is forbidden for cross-origin requests that require preliminary verification.

My js code is here:

$scope.sendRegistrationForm = function(){ var config = {headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods' : 'GET,OPTIONS', 'Access-Control-Allow-Headers' : 'X-Requested-With, Content-Type', 'Content-Type' : 'text/plain', 'Accept-Language' : 'en-US' } }; $http.get("172.20.16.45:8082/cuponza",config). success(function(data){ alert(data); }). error(function(data,status){ alert(status); }) } 

I tried to run chrome with the flag - disable-web-security , and by doing this, I could correctly see that my server side of CorsFilter was working correctly, I also received the correct answer from the server, so I put my error on the client side. when you start chrome, usually the filter on the server never works.

UPDATE: when I delete the configuration object with cors headers, I get the following error

  XMLHttpRequest cannot load 172.20.16.45:8082/cuponza . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' localhost:8100' is therefore not allowed access 

UPDATE II im showing queries as shown by chrome: initial chrome usually:

OPTIONS / cuponza HTTP / 1.1 Host: 172.20.16.45:8082 Connection: save life Access-control-request method: GET Origin: localhost: 8100 User-Agent: Mozilla / 5.0 (Windows NT 6.1; WOW64) AppleWebKit / 537.36 (KHTML, e.g. Gecko) Chrome / 36.0.1985.143 Safari / 537.36 Access-Control-Request-Headers: access-control-allow-origin, accept-language, access-control-allow-headers, access-control-allow-methods Accept: / Referer: localhost: 8100 / Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US, en; q = 0.8

launch chrome in -disable-web-security mode

GET / cuponza HTTP / 1.1 Host: 172.20.16.45:8082 Connection: save life Access-Control-Allow-Origin: * Accept-language: en-US Access-Control-Allow-Headers: X-Requested-With, Content-Type User-Agent: Mozilla / 5.0 (Windows NT 6.1; WOW64) AppleWebKit / 537.36 (KHTML, e.g. Gecko) Chrome / 36.0.1985.143 Safari / 537.36 Access-Control-Allow-Methods: GET, OPTIONS Accept: / Referer: localhost: 8100 / Accept-Encoding: gzip, deflate, sdch

0
javascript angularjs ajax spring-mvc tomcat
Aug 13 '14 at 22:37
source share
1 answer

solutions twice in your front end you should add an empty configuration object like this

var config = {}; and then add it to the angular $ Http.post call (url.config); the reason is that without an empty configuration object, angular sets the type of the POST method and checks the filter to see if the content type is text / open and not, since it is not set by default, if you send empty params, angular sets the method to , and a different logic is applied to the filter.

part II in your web.xml in the filter setup you should add the following header: access-control-allow-origin for allowed headers like this

 <init-param> <param-name>cors.allowed.headers</param-name> <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,access-control-allow-origin</param-value> </init-param> 

with this your cat is ready for CORS

+1
Aug 19 '14 at 19:49
source share



All Articles