Javascript 无法通过CORS访问amazon EC2的部分视频内容

Javascript 无法通过CORS访问amazon EC2的部分视频内容,javascript,nginx,html5-video,cors,xmlhttprequest-level2,Javascript,Nginx,Html5 Video,Cors,Xmlhttprequest Level2,我已经尽力使用stackoverflow上已有的答案来解决这个问题,但已经两天没有成功了。我正在尝试访问存储在AmazonEC2实例中的Mp4视频。这导致 无法加载XMLHttpRequest 不 “Access Control Allow Origin”标头出现在请求的服务器上 资源。因此,不允许使用源“” 通道响应的HTTP状态代码为405 我的ngnix.conf的标题如下 http{ ............ server{

我已经尽力使用stackoverflow上已有的答案来解决这个问题,但已经两天没有成功了。我正在尝试访问存储在AmazonEC2实例中的Mp4视频。这导致

无法加载XMLHttpRequest 不 “Access Control Allow Origin”标头出现在请求的服务器上 资源。因此,不允许使用源“” 通道响应的HTTP状态代码为405

我的ngnix.conf的标题如下

http{
       ............
        server{
                location / {
                        if ($request_method = 'OPTIONS') {
                                add_header 'Access-Control-Allow-Origin' '*';
                                add_header 'Access-Control-Allow-Credentials' 'true';
                                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
                                add_header 'Access-Control-Max-Age' 1728000;
                                add_header 'Content-Type' 'text/plain charset=UTF-8';
                                add_header 'Content-Length' 0;
                                return 204;
                        }
                        if ($request_method = 'POST') {
                                add_header 'Access-Control-Allow-Origin' '*';
                                add_header 'Access-Control-Allow-Credentials' 'true';
                                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
                        }
                        if ($request_method = 'GET') {
                                add_header 'Access-Control-Allow-Origin' '*';
                                add_header 'Access-Control-Allow-Credentials' 'true';
                                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
                        }
               }
       }


}
我的客户端javascript代码如下所示:

var createCORSRequest = function(method, url) {
      var xhr = new XMLHttpRequest();
      if ("withCredentials" in xhr) {
        // Most browsers.
        xhr.open(method, url, true);

        xhr.setRequestHeader('Access-Control-Allow-Methods', "HEAD, GET, POST, PUT, DELETE, OPTIONS");
        xhr.setRequestHeader('Content-type', 'text/plain');
        xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
      }  else {
        // CORS not supported.
        xhr = null;
      }
      return xhr;
    };

    var url = <videoURL> ;
    var method = 'GET';
    var xhr = createCORSRequest(method, url);

    xhr.onload = function() {
      // Success code goes here.
    };

    xhr.onerror = function() {
      // Error code goes here.
    };

    xhr.send();
var createCORSRequest=函数(方法,url){
var xhr=new XMLHttpRequest();
如果(xhr中的“带凭证”){
//大多数浏览器。
open(方法、url、true);
setRequestHeader('Access-Control-Allow-Methods',“HEAD、GET、POST、PUT、DELETE、OPTIONS”);
setRequestHeader('Content-type','text/plain');
setRequestHeader('Access-Control-Allow-Origin','*');
}否则{
//不支持CORS。
xhr=null;
}
返回xhr;
};
var url=;
var方法='GET';
var xhr=createCORSRequest(方法,url);
xhr.onload=函数(){
//成功代码在这里。
};
xhr.onerror=函数(){
//错误代码在这里。
};
xhr.send();

CORS在很大程度上是服务器所做的事情,您无需在客户端执行任何操作,也无需在客户端执行任何操作。有一些异国情调的预装和认证的cleint端边缘案例,但在大多数情况下,实现CORS的服务器意味着普通的旧ajax代码可以正常工作。无论如何,“访问控制允许源代码”永远都不应该在ajax调用中设置。简言之:如果它还不起作用,它永远不会起作用。!!嗯,我还没有做好失败的准备。丹达维斯。尽管如此,感谢您的评论应答代码405表示“不允许使用方法”,这似乎对“获取”请求有怀疑。这似乎可能是服务器配置问题。您可能可以解决这个问题,因为听起来像是您控制了服务器。尝试查看服务器返回的实际标头。浏览器通常在其开发工具中有这样做的方法。而且你可能不需要在你的请求中设置标题。同样,如上所述,这个问题实际上与html5视频无关。它只是获取一块数据。如果您有一个
标记,并且正在将其src属性设置为跨源URL,您可能需要为其设置其跨源属性,以便为您提供对视频数据的完全同源访问。如果可以,请将javascript文件与mp4放在同一服务器中,并在html文件中说
,它应该覆盖跨源问题。