Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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';
like image 873
dgzz Avatar asked Sep 20 '26 20:09

dgzz


1 Answers

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');
like image 96
mico Avatar answered Sep 22 '26 14:09

mico



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!