Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 选项405(不允许使用方法)_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 选项405(不允许使用方法)

Javascript 选项405(不允许使用方法),javascript,php,jquery,ajax,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, c

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

$.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.
很明显,我的服务器没有
访问控制允许来源
选项
对吗?但是,
filtercontroller.php
的前两行是:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
我尝试过几种不同的解决方案,但没有一种对我有效。

在将任何输出发送到浏览器之前必须放置
标题()
,一旦将输出发送到浏览器,
标题()
实际上会抛出一个PHP警告,如果您没有看到,则不会显示或跟踪该警告

或者,您也可以通过.htaccess来完成,它将在任何PHP脚本之前被处理。这是一个假设,您正在为您的Web服务器使用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"

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

  • web.config
    文件中可能有一个显式的
    。(在某些情况下,这是默认设置)
  • WebDAV模块是,您应该添加

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

访问控制允许头可能不允许*作为接受值,这取决于服务器/客户端实现-405通常由web服务器本身发布。因此,您可以检查web服务器的配置。@MjrKusanagi在本例中是不相关的--Access Control Allow Origin:*是一个可接受的值。只是确认一下--该php脚本顶部没有其他内容,对吗?是的,文件的第一行是
头是第一行代码:你尝试了.htaccess方法吗?服务器没有运行apache。这个答案应该可以解决问题,基本上是我发布的内容,但azure除外。非常感谢你。更改remove name解决了405错误,但没有解决Access Control blah blah错误,该错误是通过遵循您提供的链接并添加自定义标头解决的。享受你的赏金吧。