0

I want to get a personal page after a login in a PhoneGap project. The login part is processed via JQuery $.post. Next, the app should load the personal JSON schedule (server reads cookie JSESSIONID).

var login = "example_user";
var password= "example_password";
$.post('https://example.com/login.php', {wu_loginname:login,wu_password:password}, function(data) {    
//load the schedule.php page with the cookie received on the login.php page.    
}

Is it possible to get and write the JSESSIONID cookie with PhoneGap (or JQuery)?

Jan Koekepan
  • 81
  • 1
  • 9

2 Answers2

0

As long as it isn't a HTTP only cookie (http://blog.codinghorror.com/protecting-your-cookies-httponly/), it should be easily readable with javascript, see: "What is the shortest function for reading a cookie by name in JavaScript?" for code on how to read a cookie in javascript, or use a plugin. If JSESSIONID is server only, then you will have to write or find a plugin that can intercept the network call to retrieve the data from the headers.

Edit: Getting the cookie

function readCookie(name) {
    var allCookies = document.cookie.split('; ');
    var cookies = {};

    for (var i = allCookies.length - 1; i >= 0; i--) {
        var splitCookie = allCookies[i].split('=');
        cookies[splitCookie[0]] = splitCookie[1];
    }

    return cookies[name];
}

var login = "example_user";
var password= "example_password";
$.post('https://example.com/login.php', {wu_loginname:login,wu_password:password}, function(data) {    
   var sessId = getCookie('JSESSIONID');
   // Do something with the cookie
}
Community
  • 1
  • 1
Kris Erickson
  • 33,454
  • 26
  • 120
  • 175
  • Its not a server only cookie. I tried to read it with javascript, but it's a PhoneGap project. So I'm not sure _how_ to read it. – Jan Koekepan Nov 09 '15 at 16:26
  • @JanKoekepan it's PhoneGap, so you can read it with Javascript. The links I posted should show you how to read it, but I've updated the answer with Q&D cookie reader sample code. BTW there is such a thing as HTTP Only cookies which CANNOT be read by javascript (http://blog.codinghorror.com/protecting-your-cookies-httponly/) that is what I was asking. This is generally a setting in your web server platform (I don't know what middleware platform you are using so I don't know how that is set for you). – Kris Erickson Nov 09 '15 at 18:18
  • Oh ok, in that way. It's a cookie that could be read with Javascript. So it's not a HTTP only cookie (I did not know that ;p). – Jan Koekepan Nov 09 '15 at 18:31
0

Found the solution, simple as this :|.

var login = "example_user";
var password= "example_password";
$.post('https://example.com/login.php', {wu_loginname:login,wu_password:password}, function(data) {    
          $.get("https://example.com/schedule.php", function(result){
          alert(JSON.stringify(result));
    });     
}
Jan Koekepan
  • 81
  • 1
  • 9