简体   繁体   中英

How do I change the value of one cookie in 'Cookie' (in the header field) with XMLHttpRequest in JavaScript?

i am developing an app using phonegap for mobile platforms. For that im using js and so need to do many ajax calls. What i need to do is add a cookie (or modify its value if its already present) in the request header of an XMLHttpRequest.
I know it says here that its not possible - How do I SET a Cookie (header) with XMLHttpRequest in JavaScript?
But one seems to be able to modify the value of the 'Cookie' field in request headers, before making the xhr call, as its given in the answer here: http://developer.appcelerator.com/question/120728/set-cookies-using-setrequestheader--crashed-

It says that we can set the 'Cookie' field of the xhr req header for android and iphone in different ways like this:

if (Titanium.Platform.name == 'iPhone OS')  
{
xhr.setRequestHeader("Cookie", "cookie1=value1");
xhr.setRequestHeader("Cookie", "cookie2=value2");
}
else
{
var cookieString = "cookie1=value1; cookie2=value2;";
xhr.setRequestHeader('Cookie', cookieString);
}

but im just using js, i cant use methods/classes for iphone/android. what i need to know is, if the browser sends a 'Cookie' header something like

xyz_cookie=vd0dKmD4mT; qwe=13aef60f7d0845 xyz_cookie=vd0dKmD4mT; qwe=13aef60f7d0845 can i add another cookie "abc=1317331740;" by using the xhr.setRequestHeader method in a way that the browser sends all 3 cookies

xyz_cookie=vd0dKmD4mT; qwe=13aef60f7d0845 abc=1317331740;
in the cookie field in the request header of xhr?
If yes, pls tell me how?

xhr.setRequestHeader cant be used to set Cookie according to w3c recommendations. i guess thats why it was failing. :-( Also that cookies dont work on phonegap android apps. so that was adding to the document.cookie confusion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM