My Phonegap app does not send back JSESSIONID.

14 views
Skip to first unread message

Seif E

unread,
Aug 29, 2016, 11:22:47 AM8/29/16
to phonegap

I have a simple phonegap app that allows users to authenticate with username and password, the server sends back JSESSIONID. However when I call a web service that shows user's courses upon login, server responds with "401 authentication required" meaning the JSESSIONID was not sent back to the server and the server does not know the user is already authenticated.


This is the phonegap code where the user authenticates (by using just username for test):

 

$(document).ready(function(){

    $('#loginbutton').on('click', function(){

    var useremail= $("#email").val();

    //var password= $("#password").val();

       

    $.ajax({

                url: baseurl+ "/rest/login",

                method: 'POST',

                contentType: 'application/x-www-form-urlencoded',

                dataType: "json",

                data: { id: useremail },

                success: function (result) {

                if(result.status=="OK"){

                $.mobile.changePage('#courselistpage', {

              transition: "slide"

           });

                } else {

                alert('loginerror');

                }

                }, error: function (e) {

                    alert(JSON.stringify(e));

                }

            });

        // courselist=[{'id':1, "name": "course1"}, {'id':2, "name":'course2'}, {'id':3, 'name': 'course3'}];

        // $.mobile.changePage('#courselistpage', {

        //     transition: "slide"

        // });

    });

});

 

This is the part that shows courses upon login - this is where server responds with authentication required.

 

$(document).on("pagebeforeshow",'#courselistpage', function(){

  $.ajax({

                url: baseurl+ "/rest/courses" ,

                method: 'GET',

                contentType: 'application/json',

                dataType: "json",

                success: function (result) {

                    courselist=result;

                    for(var ii=0; ii<courselist.length; ii++){

 

 

  $('.courselistview').append('<li><a href="#" onclick="courselistclick('+ courselist[ii].id+','+courselist[ii].name+')">'+ courselist[ii].name +'</a></li>');

    $('.courselistview').listview('refresh');

    }

                }, error: function (e) {

                    alert(JSON.stringify(e));

                }

            });

  // courselist=[{'id':1, "name": "course1"}, {'id':2, "name":'course2'}, {'id':3, 'name': 'course3'}];

   

});

 

Do I need to explicitly mention JSESSIONID in the second request? or is there a way to work around this in anyway? everything is tested successfully when I test using Postman.

 

 

this is a screenshot of the "401 authentication required" I response I receive from the server when I ask for a list of courses.

 

Capture.JPG

Kerri Shotts

unread,
Aug 30, 2016, 12:14:39 PM8/30/16
to phonegap
Is JSESSIONID a cookie or a header? If the former (and you are using CORS), you need to specify `withCredentials: true` on your XHR object. See the jQuery docs for help with that (http://api.jquery.com/jquery.ajax/). If the latter, then you'll need to send the header with each request.

On Monday, August 29, 2016 at 10:22:47 AM UTC-5, Seif E wrote:

...

Reply all
Reply to author
Forward
0 new messages