Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OPTIONS 405 (Method Not Allowed)

So I'm attempting to get a progress bar on file uploads on my site. If I simply upload the resource

$.ajax({
    url: $rootScope.URL,  //Server script to process data
    type: 'POST',
    beforeSend: beforeSendHandler,
    success: completeHandler,
    error: errorHandler,
    data: formData,
    cache: false,
    contentType: false,
    processData: false
});

It works perfectly, however if I add the event to listen to progress:

$.ajax({
    url: $rootScope.URL,  //Server script to process data
    type: 'POST',
    xhr: function() {  // Custom XMLHttpRequest
        var myXhr = $.ajaxSettings.xhr();
        if(myXhr.upload){ // Check if upload property exists
            myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload
        }
        return myXhr;
    },
    beforeSend: beforeSendHandler,
    success: completeHandler,
    error: errorHandler,
    data: formData,
    cache: false,
    contentType: false,
    processData: false
});

I get:

OPTIONS myserver.com/controller/filtercontroller.php? 405 (Method Not Allowed)
  jQuery.ajaxTransport.send 
  jQuery.extend.ajax    
  (anonymous function)  
  jQuery.event.dispatch 
  jQuery.event.add.elemData.handle  
XMLHttpRequest cannot load myserver.com/controller/filtercontroller.php?. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. The response had HTTP status code 405.

So obviously my server doesn't have Access-Control-Allow-Origin and OPTIONS right? But the top 2 lines of filtercontroller.php are:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');

I've tried several different solutions and none have worked for me.

like image 561
user3194367 Avatar asked Jun 01 '15 14:06

user3194367


People also ask

What does 405 Method Not Allowed mean?

The HyperText Transfer Protocol (HTTP) 405 Method Not Allowed response status code indicates that the server knows the request method, but the target resource doesn't support this method. The server must generate an Allow header field in a 405 status code response.

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.

How do I fix 405 Method not allowed in Postman?

If you are certain you need a POST request, chances are, you're just using the wrong endpoint on the server. Again, this can only be solved if you have proper documentation to show what methods are supported for each endpoint.


1 Answers

So, first of all I do not think it has anything to do with your CORS configuration, as it would output different errors. Looking into what can cause the error you are receiving in the context of Azure/IIS I found the following possibilities:

  • You might have an explicit <remove name="OPTIONSVerbHandler" /> in your web.config file. (This is the default in certain cases).
  • The WebDAV module is interfering and you should add <remove name="WebDAVModule"/>

Lastly I just found this answer which might offer some different solutions where you do not set the CORS headers in your PHP file, but instead set them in the server configuration.

like image 93
David Mulder Avatar answered Sep 30 '22 21:09

David Mulder