Unable to redirect to external URL from Java

27 views Asked by At

I try to redirect to an EXTERNAL url with

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  response.addHeader("Access-Control-Allow-Origin", "*");
  response.sendRedirect("https://google.com");
}

but it got error:

Access to XMLHttpRequest at 'https://google.com/' (redirected from 'http://localhost:8081/xxx') from origin 'http://localhost:8081' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I know that the API or remote resource must set the header, so I set header before I call sendRedirect. But why it seems doesn't work. In fact, I found "Access-Control-Allow-Origin" doesn't set to new redirect request at all, I wonder how to correctly set header to redirect to an External url.

0

There are 0 answers