How to call a http service which is redirecting, using jquery ajax with header?

How to call a http service which is redirecting, using jquery ajax with header?

Hi,

I have 2 Java Application ( App1 App2 ) deployed in a Tomcat Server.
both this Application has one REST(http) Service.

Say,   SR1:    http://192.168.0.1:8080/App1/rest/customer

          SR2:   http://192.168.0.1:8080/App2/rest/customer

 

And, the concept of this if you call this SR1, it will redirect the request to SR2.

CORS  for this Server is activated.

 

  1. < filter >
  2.    < filter-name > CorsFilter </ filter-name >
  3.    < filter-class > org.apache.catalina.filters.CorsFilter </ filter-class >
  4.    < init-param >
  5.          < param-name > cors.allowed.origins </ param-name >
  6.          < param-value > * </ param-value >
  7.    </ init-param >
  8.    < init-param >
  9.          < param-name > cors.allowed.methods </ param-name >
  10.          < param-value > GET,POST,HEAD,OPTIONS,PUT </ param-value >
  11.    </ init-param >
  12.    < init-param >
  13.          < param-name > cors.allowed.headers </ param-name >
  14.          < param-value > Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers </ param-value >
  15.    </ init-param >
  16.    < init-param >
  17.          < param-name > cors.exposed.headers </ param-name >
  18.          < param-value > Access-Control-Allow-Origin,Access-Control-Allow-Credentials </ param-value >
  19.    </ init-param >
  20.    < init-param >
  21.          < param-name > cors.support.credentials </ param-name >
  22.          < param-value > true </ param-value >
  23.    </ init-param >
  24.    < init-param >
  25.          < param-name > cors.preflight.maxage </ param-name >
  26.          < param-value > 10 </ param-value >
  27.    </ init-param >
  28. </ filter >
  29. < filter-mapping >
  30.    < filter-name > CorsFilter </ filter-name >
  31.    < url-pattern > /* </ url-pattern >
  32. </ filter-mapping >

 

Now, I'm calling this SR1 using Jquery Ajax from Chrome. And the request is redirecting to SR2 to properly.

 

ISSUE:

But, the issue is when I'm calling SR1 with a header in my request, then first, OPTION call to SR1 is happening fine. Actual REQUEST is also going to SR1 properly, getting the redirection URL from there, but then it's unable to go to SR2.


ERROR MESSAGE:

XMLHttpRequest cannot load http://192.168.0.1:8080/App1/rest/customer. The request was redirected to 'http://192.168.0.1:8080/App2/rest/customer', which is disallowed for cross-origin requests that require preflight.

MY CODE:

  1. $.ajax({ url: " http://192.168.0.1:8080/App1/rest/customer", dataType: 'APPLICATION/JSON', type: "GET", beforeSend: function(xhr){xhr.setRequestHeader('X-Requested-With', 'test-value');}, success: function() { alert('Success!'); } });

Question:
Now, the question is, how to call this service, which is redirecting, from ajax jquery along with a header? Just to clarify, the 1st service is going to need this header, not the redirected service.

Thank you!
Anijit