gpt4 book ai didi

javascript - CORS 不适用于 Chrome/Firefox 和 Apache

转载 作者:搜寻专家 更新时间:2023-11-01 04:36:03 24 4
gpt4 key购买 nike

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

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

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://x.x.x.x/validateCustomerID。请求的资源上不存在 'Access-Control-Allow-Origin' header 。因此不允许访问来源 'null'。响应的 HTTP 状态代码为 403。”

  • 在 Firefox 中:

“跨源请求被阻止:同源策略不允许读取位于 https://x.x.x.x/validateCustomerID 的远程资源。(原因:CORS 请求失败)。”

在我的浏览器中没有从服务器接收到响应 header ,我认为这是 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

关于javascript - CORS 不适用于 Chrome/Firefox 和 Apache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33168189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com