JavaScript CORS issue on java web application cors issue java,cors issue java spring,cors problem java,

The backend is developed by Spring MVC and hosted in tomcat.

When I use postman dev tool in Chrome to test the endpoint (Restful), everything is working fine. The "Access-Control-Allow-Origin", "*" is successfully added in the server. like below screenshot enter image description here

But when I wrote the Ajax to make the same request. Because browser has the Origin Policy (postman has no this policy), it will first send an HTTP OPTIONS request header to the server to determine whether the actual request is safe to send. like below screenshot. enter image description here

The response header is quite different to the one I got from postman, and there is no "Access-Control-Allow-Origin", "*" in the header. I think the server just can't accept the options request.

I have tried to add the filter in tomcat web.xml by looking this link: http://enable-cors.org/server_tomcat.html

It's Only working perfectly by using dev tool like curl and postman which has no Origin policy. But it's not working on the browser by Ajax.

Please see the below filter class

public class SimpleCORSFilter implements Filter {
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
System.out.println("test123");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");
        chain.doFilter(req, res);
    }
    public void init(FilterConfig filterConfig) {}
    public void destroy() {}
}

I use the System.out.println("test123"); in the filter, when using postman or curl, it printed the 'test123' in the console, but didn't print when using browser, so I guess the browser send the OPTIONS request, and the filter didn't intercept it, or other things intercept the request from browser first?

Can anyone help me please? Thanks.

Answer:1

Is there a way to find all the child elements of a div that has id like:- child_*_of_parent HTML <div id="parent"> <div id=child_1_of_parent>...</div> <div id=...

Is there a way to find all the child elements of a div that has id like:- child_*_of_parent HTML <div id="parent"> <div id=child_1_of_parent>...</div> <div id=...

I'm using Angular(UI)-Google-Maps (2.1.5) and AngularJS (1.3.13) I want a marker that is always in the center of the map. The problem is that the marker only updates its position when I stop dragging....

I'm using Angular(UI)-Google-Maps (2.1.5) and AngularJS (1.3.13) I want a marker that is always in the center of the map. The problem is that the marker only updates its position when I stop dragging....

  1. angular google maps api
  2. angular google maps component
  3. angular google maps example
  4. angular google maps autocomplete
  5. angular google maps npm
  6. angular google maps geocoding
  7. angular google maps heatmap
  8. angular google maps directions
  9. angular google maps marker
  10. angular google maps types
  11. angular google maps add marker
  12. angular google maps not showing
  13. angular google maps documentation
  14. angular google maps search box example
  15. angular google maps cluster example
  16. angular google maps change marker icon
  17. angular google maps directions example
  18. angular google maps get current location
  19. angular google maps tutorial
  20. angular google maps github

i'm loading somthing like this in DOM using AJAX: <div class="elm_class"></div> <div class="elm_class"></div> <div class="elm_class"></div> and then I need to ...

i'm loading somthing like this in DOM using AJAX: <div class="elm_class"></div> <div class="elm_class"></div> <div class="elm_class"></div> and then I need to ...

  1. protractor wait for multiple elements

<p>1</p> <p>2</p> <p>3</p> I want to hide / delete the second <p> without using .eq(), or CSS nth-child(2), I just want to catch the value if it's 2 then ...

<p>1</p> <p>2</p> <p>3</p> I want to hide / delete the second <p> without using .eq(), or CSS nth-child(2), I just want to catch the value if it's 2 then ...

  1. selector based on text