Cannot get cookie
Greetings,
I'm trying to get the PHPSESSID cookie, but it keeps returning null. I'm certain the page login.php gives out a cookie as I've seen the cookie in my web browser/Firebug. Here is the cookie from Firebug: PHPSESSID=7atro6i6rc7ugprt4841g5nej2
Appcelerator (Android Simulator 2.2 API with 1.4.0 sdk) gives an error "cannot call method 'getValue' of null".
Here is my code:
var xhr=Titanium.Network.createHTTPClient();
xhr.onload=function(){
if(xhr.responseText=="OK"){
alert(xhr.getCookie("PHPSESSID").getValue()); //<----- does not work
//Apparently xhr.getCookie("PHPSESSID") is null
var win_home=Titanium.UI.createWindow({
url:'wins/hoome.js',
backgroundColor:'#CCC'
});
alert(Titanium.App.eoh_cookie);
win_home.open();
}
};
xhr.onerror=function(){
alert('There was an error processing your request. Please try again.');
}
xhr.open("POST","http://10.0.2.2/Dropbox/www/ajax/login.php");
var data={};
data.uname=uname.value;
data.pword=pword.value;
xhr.send(data);
Am I getting the cookie in the right place? Any insight greatly appreciated.
Many thanks in advance,
3 Answers
-
Ok, I've managed to resolve this problem:
Get a cookie with:
var cookie=xhr.getResponseHeader("Set-Cookie"); //parse cookie if necessary
Set a cookie withi:
... xhr.setRequestHeader("Cookie",cookie); //just before send xhr.send(data);
Many thanks for all the help guys,
-
set cookie should be
cdb_auth="XXXXX"
cdb_sid="XXXX"xhr.setRequestHeader('Cookie', cdb_auth);
xhr.setRequestHeader("Cookie", cdb_sid);since cookie transfers as http request header
-
this will get you all of the cookies, I guess then you can parse the string to get a specific one
xhr.onload=function(){ if(xhr.responseText=="OK") { Ti.API.info( "cookies: " + xhr.getResponseHeader('Set-Cookie') ); }