javascript 选项 405(不允许的方法)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30575539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OPTIONS 405 (Method Not Allowed)
提问by user3194367
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:
很明显我的服务器没有Access-Control-Allow-Origin
,OPTIONS
对吗?但前两行filtercontroller.php
是:
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.
我尝试了几种不同的解决方案,但没有一个对我有用。
采纳答案by David Mulder
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:
因此,首先我认为这与您的 CORS 配置没有任何关系,因为它会输出不同的错误。查看可能导致您在 Azure/IIS 上下文中收到的错误的原因,我发现了以下可能性:
- You might have an explicit
<remove name="OPTIONSVerbHandler" />
in yourweb.config
file. (This is the default in certain cases). - The WebDAV module is interferingand you should add
<remove name="WebDAVModule"/>
- 您
<remove name="OPTIONSVerbHandler" />
的web.config
文件中可能有一个显式。(这是某些情况下的默认设置)。 - WebDAV 模块有干扰,您应该添加
<remove name="WebDAVModule"/>
Lastly I just found this answerwhich 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.
最后,我刚刚找到了这个答案,它可能会提供一些不同的解决方案,您无需在 PHP 文件中设置 CORS 标头,而是在服务器配置中设置它们。
回答by Augwa
The header()
must be placed before ANY output is sent to the browser, once output is sent to the browser the header()
will actually throw a PHP warning which if you aren't seeing isn't being displayed or tracked.
在header()
之前的任何输出发送到浏览器必须放置,一旦输出发送到浏览器header()
实际上将抛出其如果不被显示或跟踪您没有看到一个PHP警告。
Alternatively you can also do it through .htaccess which would be processed before any of your PHP scripts. This is going on the asumption that you are using apache for your webserver.
或者,您也可以通过 .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"