如何设置cookie的值与Ajax请求? [英] How to set Cookie value with ajax request?
问题描述
$.ajax({
type: "GET",
url: "http://XYZ.com",
cache: false,
setCookies: "lkfh89asdhjahska7al446dfg5kgfbfgdhfdbfgcvbcbc dfskljvdfhpl",
crossDomain: true,
dataType: 'json',
success: function (data) {
alert(data);
});
我想设置cookie的值,而Ajax请求
。但它不工作。
请告诉我如何在头设置cookie的值。
I want to set the cookie value while ajax request
. but it doesn't work.
Please tell me how to set cookie value in header.
推荐答案
基本上,Ajax请求以及同步请求自动发送您的文档的cookies。所以,你需要设置你的cookie文件,不索取。然而,你的要求是跨域,事情变得更加复杂。此基础上<一个href="http://stackoverflow.com/questions/10230341/http-cookies-and-ajax-requests-over-https/10249123">this回答,另外设置Cookie文件,你应该允许其发送到跨域环境:
Basically, ajax request as well as synchronous request sends your document cookies automatically. So, you need to set your cookie to document, not to request. However, your request is cross-domain, and things became more complicated. Basing on this answer, additionally to set document cookie, you should allow its sending to cross-domain environment:
type: "GET",
url: "http://example.com",
cache: false,
// NO setCookies option available, set cookie to document
//setCookies: "lkfh89asdhjahska7al446dfg5kgfbfgdhfdbfgcvbcbc dfskljvdfhpl",
crossDomain: true,
dataType: 'json',
xhrFields: {
withCredentials: true
},
success: function (data) {
alert(data);
});
这篇关于如何设置cookie的值与Ajax请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!