选项405(不允许的方法)

use*_*367 14 javascript php ajax jquery

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

$.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
});
Run Code Online (Sandbox Code Playgroud)

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

$.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
});
Run Code Online (Sandbox Code Playgroud)

我明白了:

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.
Run Code Online (Sandbox Code Playgroud)

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

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
Run Code Online (Sandbox Code Playgroud)

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

Dav*_*der 6

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

  • <remove name="OPTIONSVerbHandler" />web.config文件中可能有明确的内容.(在某些情况下,这是默认值).
  • WebDAV模块正在干扰,您应该添加<remove name="WebDAVModule"/>

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