LocalStorage and JSON


#1

Hi there,

    var data = {
    "username": $scope.loginData.username,
    "password": $scope.loginData.password
    };
   
$http.post("http://url/to/login.php", data,{"username": $scope.loginData.username,"password": $scope.loginData.password})
    .success(function(data){
       localStorage.setItem("LoggedOnProfile", JSON.stringify(data));
       console.log(localStorage.getItem("LoggedOnProfile")); //returns [{id:..., firstname:...,}]
       var LoggedOnProfile = JSON.parse(localStorage.getItem("LoggedOnProfile"));
       console.log(LoggedOnProfile.firstname); //returns undefined
    });

Why does the console.log return ‘undefined’? Everthing works, including logging the .getItem…

Thanks in advance,

Jan


#2

Where “firstname” is defined?


#3

It looks allright. I tried it on console it works.Look at this fiddle.
fiddle


#4

Thanks for helping!

It works now, didn’t change anything in my code… I’m only at a different location (work/home). Maybe it has to do something with that?

Thanks again.