选项405(不允许的方法)

所以我试图在我的网站上获取文件上传的进度条。 如果我只是上传资源

$.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 }); 

它工作得很好,但是如果我添加事件来听取进度:

 $.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 }); 

我明白了:

 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. 

显然我的服务器没有Access-Control-Allow-OriginOPTIONS吗? 但filtercontroller.php的前两行是:

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

我尝试了几种不同的解决方案,但没有一种能为我工作。

因此,首先我不认为它与您的CORS配置有任何关系,因为它会输出不同的错误。 查看可能导致您在Azure / IIS上下文中收到错误的原因,我发现了以下可能性:

  • 您可能在web.config文件中有一个显式的 。 (在某些情况下,这是默认值)。
  • WebDAV模块正在干扰 ,您应该添加

最后我发现这个答案可能会提供一些不同的解决方案,你不在PHP文件中设置CORS头,而是在服务器配置中设置它们。

在将任何输出发送到浏览器之前必须放置header() ,一旦输出发送到浏览器, header()将实际抛出一个PHP警告,如果你没有看到没有显示或跟踪。

或者,您也可以通过.htaccess执行此操作,该任务将在任何PHP脚本之前处理。 这是假设你正在为你的网络服务器使用apache。

 Header set Access-Control-Allow-Origin "*" # below line shouldn't be needed as by default all of those are allowed, # if you were doing PUT, TRACE, DELETE, etc then you would need it Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"