问题描述
看来,类似的事情已经已经在计算器讨论,但我无法找到完全一样的。
Seems that something similar already has been discussed on stackoverflow, but i could not find exactly the same.
我想送饼干与CORS(跨域资源共享),但它不工作。
I am trying to send Cookie with CORS(Cross-origin resource sharing), but it is not working.
这是我的code。
$.ajax(
{
type: "POST",
url: "http://example.com/api/getlist.json",
dataType: 'json',
xhrFields: {
withCredentials: true
},
crossDomain: true,
beforeSend: function(xhr) {
xhr.setRequestHeader("Cookie", "session=xxxyyyzzz");
},
success: function(){
alert('success');
},
error: function (xhr) {
alert(xhr.responseText);
}
}
);
我没有看到这个cookie的请求头。
I dont see this cookie in request HEADER.
推荐答案
您不能设置或读取的Cookie在CORS通过JavaScript请求。虽然CORS允许跨域请求,饼干仍受到浏览器的同源策略,这意味着来自同一产地唯一的页面可以读/写的cookie。 withCredentials
只意味着远程主机设置任何cookie被发送到远程主机。您必须使用设置Cookie
头设置从远程服务器中的cookie。
You cannot set or read cookies on CORS requests through JavaScript. Although CORS allows cross-origin requests, the cookies are still subject to the browser's same-origin policy, which means only pages from the same origin can read/write the cookie. withCredentials
only means that any cookies set by the remote host are sent to that remote host. You will have to set the cookie from the remote server by using the Set-Cookie
header.
这篇关于跨域POST请求不发送的cookie的Ajax jQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!