Skip to content Skip to sidebar Skip to footer

Jquery Ajax Request To An Static Html Resource In Nginx Causes A "405 Not Allowed"

I have an Nginx with a simple index.thml running within a Docker. Everything works fine if I call it from a browser with http://localhost:8979/index.html, but when I call it from J

Solution 1:

Try this one:

map $request_method $options_content_type {
    OPTIONS    "text/plain";
}
map $request_method $options_content_length {
    OPTIONS    0;
}
server { 
    listen 80;
    location / {
        if ($request_method = OPTIONS) { return 204; }
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT';
        add_header 'Access-Control-Allow-Headers' 'User-Agent,Keep-Alive,Content-Type'; 
        add_header Content-Type $options_content_type;
        add_header Content-Length $options_content_length;
    }
}

Beware! You are declaring GET, POST, OPTIONS, DELETE and PUT as allowed HTTP methods, but in fact nothing except GET (and now OPTIONS) will work with this configuration. Do you really need all the other methods?

Post a Comment for "Jquery Ajax Request To An Static Html Resource In Nginx Causes A "405 Not Allowed""