gpt4 book ai didi

jquery - 使用 withCredentials 和客户端证书的 jQuery 的 CORS 请求失败

转载 作者:行者123 更新时间:2023-12-03 22:55:59 24 4
gpt4 key购买 nike

我不明白为什么这个 CORS 请求无法返回数据。

我在后端使用 Catalyst MVC,使用 Firefox 24.0 作为浏览器。 jQuery 1.9.1。请注意以下事项:

  1. otherdomain.com 需要客户端证书。
  2. 直接点击资源会返回预期的数据。 ( https://otherdomain.com/resource/1 ) 返回正确的数据。

我有一个简单的页面来测试请求:

<script type='text/javascript'>
function get_data() {
console.log("running");
$.ajax({
url: "https://otherdomain.com/resource/1",
dataType: 'json',
type: 'GET',
xhrFields: {
'withCredentials': true
},
crossDomain: true
}).success(function(data) {
console.log(data)
$('#output').html(data);
}).error(function(xhr, status, error) {
alert("error");
console.log(xhr);
});
}

$(document).ready(function() {
get_data();
});
</script>

</script>

这是我的请求 header :

GET /resource/1 HTTP/1.1
Host: otherdomain.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Referer: https://mydomain.com/test.html
Origin: https://mydomain.com
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

这是我的响应 header 。 (从 Firebug 控制台查看源代码的副本)我在 Catalyst 调试输出中看到该请求被视为 200 OK 并且内容已发送。

HTTP/1.1 200 OK
Date: Mon, 28 Oct 2013 19:31:08 GMT
Server: HTTP::Server::PSGI
Vary: Content-Type
Content-Length: 653
Content-Type: application/json
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 1800
X-Catalyst: 5.90030
Via: 1.1 otherdomain.com

并且 ajax 调用抛出错误:

readyState: 0
responseText: ""
status: 0
statusText: "error"

firebug 将请求事件的响应正文显示为空,尽管它是 200 OK。

我认为使用“withCredentials”时需要执行飞行前请求,但我没有看到通过 firebug 发送选项。

此外,我看不到我的请求添加了任何 Access-Control-Request-Header,因此我不会返回任何 Access-Control-Allow-Headers来自服务器。

现在,Catalyst 的前端是 Apache2,我在虚拟主机中使用 proxypass 将请求发送到 localhost:8080 上的 Catalyst。我不确定这是否有任何影响,但认为这可能很重要。但它对浏览器应该是透明的。

感谢您的帮助!

最佳答案

  1. GET 请求未经预检。 See Here
  2. 响应凭据请求时,服务器必须指定域,并且不能使用通配符。 (不能是 Access-Control-Allow-Origin: *)。 See Here

关于jquery - 使用 withCredentials 和客户端证书的 jQuery 的 CORS 请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19644015/

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