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.
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.
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.
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.
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:
<remove name="OPTIONSVerbHandler" />
in your web.config
file. (This is the default in certain cases).<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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With