[英]OPTIONS 405 (Method Not Allowed)
所以我试图在我的网站上获取文件上传的进度条。 如果我只是上传资源
$.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');
我尝试了几种不同的解决方案,没有一种能为我工作。
在将任何输出发送到浏览器之前必须放置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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.