0

I'm trying to send an object between two pages in javascript but I'm getting undefined knowing that I'm using tomcat server here's the first function in the first page :

$http.get('rest/client/authentifier/'+$scope.nouveauClient.numerocompte + '/' + $scope.nouveauClient.mdp)
.then(function(clientresult) {
    window.localStorage.setItem("client",clientresult.data);
    parent.location = "acceuilClient.html";
})

here's the second function in the seconde page :

var user;

$scope.display = function()
{
    user =  window.localStorage.getItem('client');
    alert("amal  "+user.idclient);
}
Sébastien
  • 11,860
  • 11
  • 58
  • 78
A.Aou
  • 45
  • 1
  • 10
  • Are the URLs different? LocalStorage is domain specific from this [post](https://stackoverflow.com/questions/16144906/is-it-possible-to-use-html5-local-storage-to-share-data-between-pages-from-diffe). – Mickers Jan 29 '18 at 19:23
  • No, when I send window.localStorage.setItem("client",clientresult.data.idclient); I get it otherwise I get undefined – A.Aou Jan 29 '18 at 19:59

1 Answers1

0

You cannot store JavaScript objects as such in localStorage, only strings.

Whatever you stored in window.localStorage.setItem("client",clientresult.data);, you will not retrieve an object with properties.

When calling user.idclient, you are calling the idclient property on a string, which will yield undefined. Try

user =  window.localStorage.getItem('client');
console.log(user)

And it may output

[object Object]

What you need to do is store key/value pairs like:

window.localStorage.setItem("idclient", clientresult.data.idclient);

You might want to check if the idclient exists in clientresult.data first.

Sébastien
  • 11,860
  • 11
  • 58
  • 78