Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.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
Google chrome Firefox阻止CORS,但允许使用Chrome,使用Nginx_Google Chrome_Firefox_Nginx_Cors_Reverse Proxy - Fatal编程技术网

Google chrome Firefox阻止CORS,但允许使用Chrome,使用Nginx

Google chrome Firefox阻止CORS,但允许使用Chrome,使用Nginx,google-chrome,firefox,nginx,cors,reverse-proxy,Google Chrome,Firefox,Nginx,Cors,Reverse Proxy,我想使用XMLHttpRequest对后端RESTful API服务器进行AJAX调用。前端由Apache托管。API服务器由Django 2.0.9(HTTP,我需要使用Python)托管,HTTPS由使用自签名证书的Nginx(v1.4.6)反向代理提供 调用后端API的代码是: var formData = new FormData(); formData.append('data', 'I am data'); var xhr = new XMLHttpRequest(); xhr.o

我想使用XMLHttpRequest对后端RESTful API服务器进行AJAX调用。前端由Apache托管。API服务器由Django 2.0.9(HTTP,我需要使用Python)托管,HTTPS由使用自签名证书的Nginx(v1.4.6)反向代理提供

调用后端API的代码是:

var formData = new FormData();
formData.append('data', 'I am data');

var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://api.mysite.com/api1/', true);
xhr.onreadystatechange = function() {
    if (xhr.readyState === 4 && xhr.status === 200) {
        /* handling response */
    }
};
xhr.send(formData);
在Chrome中,它可以工作。但是,在Firefox中(我使用版本61.0.1 64位进行了测试),我得到了一个错误:
“跨源请求被阻止:同源策略不允许在https://api.mysite.com/api1/. (原因:CORS请求未成功)。“

Nginx中使用的配置文件是:

server {
    listen 8443;
    server_name localhost;
    ssl on;
    ssl_certificate /etc/ssl/certs/ssl-cert-snakeoil.pem;
    ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key;
    location / {
        proxy_pass http://localhost:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        if ($request_method = 'OPTIONS') {
            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-Expose-Headers' 'Content-Length,Content-Range';
        }
        if ($request_method = 'GET') {
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
        }
    }
}

互联网上有很多关于CORS的信息,但我似乎找不到解决这个问题的方法。非常感谢。

您有没有找到解决此问题的方法?我只在Firefox中遇到了类似的问题。如果你们中有人找到了这个问题的解决方案,我也有同样的问题。我在互联网上搜索了一些线索来帮助我解决这个问题,但运气不好。我的发现:显然CORS请求失败,出现504网关超时错误,该错误出现在Firefox控制台中,但不出现在Nginx日志中。Firefox报告OPTIONS请求成功,我想失败的是相应的GET请求(它有一个授权头)。在铬合金中,它可以完美地工作。