Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OPTIONS 405 (Method Not Allowed) regardless server sends Access-Control-Allow-Methods:OPTIONS, GET, HEAD, POST

I'm trying to make cross-domain request and my server is configured to send the following headers:

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:x-requested-with, Authorization
Access-Control-Allow-Methods:OPTIONS, GET, HEAD, POST
Access-Control-Allow-Origin:*

But when an OPTION request is made, I get OPTIONS 405 (Method Not Allowed) error.

Any Ideas what is the problem and how to fix it?

like image 536
Spadar Shut Avatar asked Nov 08 '12 11:11

Spadar Shut


People also ask

Why am I getting a 405?

A 405 Method Not Allowed Error is an HTTP response status code that indicates a web browser has requested access to one of your web pages and your web server received and recognized its HTTP method.

Why is POST method not allowed?

The Request Method' POST' Not Supported error is caused by a mismatch of the web browser configuration and the browser's URL format. In this case, the browser sends a URL request, the web server receives and recognizes the URL but cannot execute commands or grant access to the requested page.

What is a 405 code?

The 405 Method Not Allowed is an HTTP response status code indicating that the server received and recognized the specified request HTTP method, but the server rejected that particular method for the requested resource.


1 Answers

I would suggest 2 solutions:

1) If you are using WebAPI you need to implement the option method that by convention should look like:

public class XXXController : ApiController
{
    // OPTION http-verb handler
    public string OptionsXXX()
    {
        return null; // HTTP 200 response with empty body
    }

    ...
}

2) If you are not using WebAPI try to understand which part of your code triggers the OPTIONS 405 (Method Not Allowed) error for the OPTION call. In that case I would check if trying to add to the Web.config file these <customHeaders/> that works:

<configuration>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <!-- CORS temporary solution -->
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type, Authorization, Accept, X-Requested-With" />
        <add name="Access-Control-Allow-Methods" value="OPTIONS, TRACE, GET, HEAD, POST, PUT" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>
like image 96
Filippo Vitale Avatar answered Oct 13 '22 04:10

Filippo Vitale