繁体   English   中英

CORS无法在Chrome / Firefox和Apache上运行

[英]CORS not working on Chrome/Firefox and Apache

我正在尝试使用CORS在我的浏览器和Apache服务器(驻留在不同的域中)之间运行AJAX请求。

在服务器端,我在服务器的httpd.conf部分中进行了以下更改,根据“ Header set Access-Control-Allow-Origin in .htaccess中的响应不起作用 ”:

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

我的AJAX调用形式如下:

        $.ajax({
            url        :'https://x.x.x.x/validateCustomerID',
            type       : 'POST',
            cache    : false,
            crossDomain: true,
            contentType: 'application/json',
            beforeSend: function(xhr){
                    xhr.setRequestHeader("Access-Control-Allow-Methods","POST");
                    xhr.setRequestHeader("Access-Control-Allow-Headers","X-Requested-With");
                    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            },
            data       : {loginId : '12345'},
            success    : function(response){console.log("Success"+JSON.stringify(response))},
            error      : function(response){console.log("Error"+JSON.stringify(response))}
        });
    }

我也试过注释掉beforeSend()以避免预检请求,但它也没有成功。

我在Chrome和Firefox上收到的错误消息是:

  • 在Chrome中:

“XMLHttpRequest无法加载https:// xxxx / validateCustomerID 。请求的资源上不存在'Access-Control-Allow-Origin'标头。因此不允许原点'null'访问。响应的HTTP状态代码为403.”

  • 在Firefox中:

“阻止跨源请求:同源策略禁止在https:// xxxx / validateCustomerID读取远程资源。(原因:CORS请求失败)。”

我的浏览器中没有从服务器收到响应标头,我认为这是CORS必须工作的,并且服务器中的日志显示没有请求从我的浏览器到达它。

我真的很感激,如果有人在这里可以帮助我解决这个问题,因为我已经在这里呆了几天,并且已经使用了几乎所有的命中和试用方法来使这个工作成功。

这是我在site.conf中的设置,现在使用apache2在生产中工作

Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "authorization, origin, user-token, x-requested-with, content-type"
Header set Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

为了将来的参考,我强烈建议将此网站加入书签http://enable-cors.org/index.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM