选项405(不允许的方法)

时间:2015-06-01 14:00:53

标签: javascript php jquery ajax

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

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

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

2 个答案:

答案 0 :(得分:5)

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

  • 您的<remove name="OPTIONSVerbHandler" />文件中可能有明确的web.config。 (在某些情况下,这是默认设置。)
  • WebDAV模块为interfering,您应添加<remove name="WebDAVModule"/>

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

答案 1 :(得分:0)

在将任何输出发送到浏览器之前必须放置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"