从主域向子域发送AJAX调用



我知道这个问题可能已经得到了回答,但是我找到的解决方案都不适合我。

我有一个AJAX调用从example.com到api.example.com/v1,当发送请求时,没有cookie被发送。

编辑:

我的AJAX代码是

$.ajax({
type:"POST",
url:"https://api.example.com/v1/",
data: payload,
processData: false,
xhrFields: { withCredentials:true },
crossDomain: true,
xhrFields: { responseType: 'arraybuffer' },
dataType: 'binary',
contentType: 'application/octet-stream',
headers: { "Csrf-Token": csrfValue, "accept": "application/octet-stream"},
success: function(res) {
// Some code here
}
});

编辑2:

服务器在预飞行请求上设置的报头是:

access-control-allow-credentials: true
access-control-allow-headers: Content-Type, Csrf-Token, Cookie
access-control-allow-methods: OPTIONS, POST
access-control-allow-origin: https://example.com
allow: OPTIONS, POST

编辑3:

我的cookie值是:

Name: __Secure-Csrf-Token
Content: 4XAlnRWR95FKGcpYxZb6P9xxtFx1hP7XXU8rSShpnEY
Domain: .example.com
Path: /
Send for: Secure same-site connections only
Created: Wednesday, May 25, 2022 at 11:18:17 PM
Expires: When the browsing session ends

是的,我的网站使用HTTPS

好的,所以看起来我在重复xhrFields参数,所以第一次提到我设置withCredentials: true的地方被覆盖了。

最新更新