dgzz
dgzz

Reputation: 3009

Ionic/Cordova AngularJS getting request header of $http

I want to get the $http cookie request header after a request but the $http only returns a response according to the AngularJS documentation. Here is the cookie I want to get:

enter image description here

I tried adding an interceptor but I couldn't see the cookie. Here is the output from my interceptor:

enter image description here

My interceptor and some settings:

$provide.factory('myHttpInterceptor', function() {
      return {
        // optional method
        'request': function(config) {
          console.log(config);
          return config;
        }
      };
    });

    $httpProvider.interceptors.push('myHttpInterceptor');

    $httpProvider.defaults.useXDomain = true;
    //Remove text on back button
    $ionicConfigProvider.backButton.previousTitleText(false).text('  ');
    $httpProvider.defaults.withCredentials = true;
    //Setting angularjs' $http to behave as url encoded parameters and request instead of JSON
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';

Upvotes: 3

Views: 4089

Answers (2)

liima
liima

Reputation: 236

If you don't mind me asking, why do you want to read the JSESSIONID on the client side?

I can imagine that you would want to send it with every subsequent request, but that's automatically done for you if you use withCredentials = true.

Upvotes: 1

mico
mico

Reputation: 12748

You cannot use cookies in a cordova app. There are quite a many other posts on the issue in Stackoverflow and the most recent info I found on this blog, where the issue is stated in these words:

I honestly like everything about Cordova aside from the fact that you can’t use cookie based sessions.

In cordova you can though access session storage and localstorage, introduced by html5.

http://www.w3schools.com/html/html5_webstorage.asp introduces for example localstorage with words:

HTML local storage, better than cookies.

http://learn.ionicframework.com/formulas/localstorage/ has example code like this:

Use this simple AngularJS service for setting and retrieving strings or objects easily:

angular.module('ionic.utils', [])

.factory('$localstorage', ['$window', function($window) {
  return {
    set: function(key, value) {
      $window.localStorage[key] = value;
    },
    get: function(key, defaultValue) {
      return $window.localStorage[key] || defaultValue;
    },
    setObject: function(key, value) {
      $window.localStorage[key] = JSON.stringify(value);
    },
    getObject: function(key) {
       return JSON.parse($window.localStorage[key] || '{}');
    }
  }
}]);

And to use this service, just inject the $localstorage service into a controller or run function:

angular.module('app', ['ionic', 'ionic.utils'])

.run(function($localstorage) {

  $localstorage.set('name', 'Max');
  console.log($localstorage.get('name'));
  $localstorage.setObject('post', {
    name: 'Thoughts',
    text: 'Today was a good day'
  });

  var post = $localstorage.getObject('post');
  console.log(post);
});

Now, with this example in hand, you can do something similar to:

$localstorage.set('sessionId', 'yourSessionIdGoesHere');

and read

$localstorage.get('sessionId');

Upvotes: 1

Related Questions