所以我试图在我的网站上获取文件上传的进度条。如果我只是上传资源
$.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-Origin
和 OPTIONS
对吗?但是 filtercontroller.php
的前两行是:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
我尝试了几种不同的解决方案,但没有一个对我有用。
最佳答案
所以,首先我认为它与您的 CORS 配置没有任何关系,因为它会输出不同的错误。查看可能导致您在 Azure/IIS 上下文中收到的错误的原因,我发现了以下可能性:
- 你可能有一个明确的
<remove name="OPTIONSVerbHandler" />
在你的web.config
文件。 (在某些情况下这是默认设置)。 - WebDAV 模块是 interfering你应该添加
<remove name="WebDAVModule"/>
最后我找到了this answer这可能会提供一些不同的解决方案,您不在 PHP 文件中设置 CORS header ,而是在服务器配置中设置它们。
关于javascript - OPTIONS 405(方法不允许),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30575539/