Search code examples
javaangularjsgoogle-app-enginehttp-options-method

404 for OPTIONS method in Google AppEngine with Java and Angular.js


I am using Angular.js with AppEngine on Java.
For every HTTP request, Angular.js issues an OPTIONS request before actual request, which returns an HTTP 404 error for me.
Can anybody please tell me why?

In AppEngine how to enable the configuration for OPTIONS method?

edit: I created a filter to handle OPTIONS request. Now I add the Allow header with GET, POST, DELETE, PUT, OPTIONS, HEAD as value and respond with status code 200. But still it shows 404.


Solution

  • Finally got it working. I wrote the code like this:

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse resp = (HttpServletResponse) response;
        HttpServletRequest req = (HttpServletRequest) request;
        resp.setHeader("Cache-Control", "max-age=3600");
        resp.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        resp.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS");
        resp.setHeader("Access-Control-Allow-Origin", "www.myapp.com");
        resp.setHeader("Access-Control-Allow-Credentials", "true");
        if (req.getMethod().equals("OPTIONS")) { 
            resp.flushBuffer();
        } else {
            chain.doFilter(request, resp);
        }
    }
    

    I just wrote the resp.setHeader("Access-Control-Allow-Origin", "*"); before. After writing the whole headers, it started working.