我正在尝试在javascript中的两个页面之间发送一个对象,但我知道我正在使用tomcat服务器时未定义 这是第一页中的第一个函数:
$http.get('rest/client/authentifier/'+$scope.nouveauClient.numerocompte + '/' + $scope.nouveauClient.mdp)
.then(function(clientresult) {
window.localStorage.setItem("client",clientresult.data);
parent.location = "acceuilClient.html";
})
这是seconde页面中的第二个函数:
var user;
$scope.display = function()
{
user = window.localStorage.getItem('client');
alert("amal "+user.idclient);
}
答案 0 :(得分:0)
您不能在localStorage中存储JavaScript对象,只能存储字符串。
无论您在window.localStorage.setItem("client",clientresult.data);
中存储了什么,都不会检索具有属性的对象。
调用user.idclient
时,您在字符串上调用idclient
属性,这将导致未定义。试试
user = window.localStorage.getItem('client');
console.log(user)
它可能会输出
[object Object]
您需要做的是存储键/值对,如:
window.localStorage.setItem("idclient", clientresult.data.idclient);
您可能想先检查idclient
中是否存在clientresult.data
。