我正在尝试使用localStorage在页面之间传递变量。在java中我填充了List:
output.add(0, accountGroup.getAccountId());
output.add(1, accountGroup.getLevel());
output.add(2, accountGroup.getGroupName());
output.add(3, "0");
output.add(4, accountGroup.getGroupSection());
output.add(5, accountGroup.getGroupNAS());
output.add(6, nextView);
String json = new Gson().toJson(output);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
然后我尝试将每个变量存储在ajax中:
.done(function(responseJson){
dataType: "json";
localStorage.setItem('lsAccountID', $('#temp').val(responseJson[0]));
alert(localStorage.getItem('lsAccountID'));
localStorage.setItem('lsAccountLevel', $('#temp').val(responseJson[1]));
localStorage.setItem('lsGroupName', $('#temp').val(responseJson[2]));
localStorage.setItem('lsNoGroupsStarted', $('#temp').val(responseJson[3]));
localStorage.setItem('lsGroupSection', $('#temp').val(responseJson[4]));
localStorage.setItem('lsGroupNAS', $('#temp').val(responseJson[5]));
localStorage.setItem('lsNextView', $('#temp').val(responseJson[6]));
window.location.assign("SelectPerson.html");
})
这不存储正确的值(" 7"在这种情况下),这由" alert(localStorage.getItem(' lsAccountID'))显示;&# 34;返回" [对象]"而不是" 7"。我知道" 7"由" output.add(0,accountGroup.getAccountId());"存储。通过添加" System.out.println"。
答案 0 :(得分:0)
答案是使用parseJSON:
.done(function(responseJson){
dataType: "json";
var result = $.parseJSON(responseJson);
localStorage.setItem('lsAccountID', result[0]);
localStorage.setItem('lsAccountLevel', result[1]);
localStorage.setItem('lsGroupName', result[2]);
localStorage.setItem('lsNoGroupsStarted', result[3]);
localStorage.setItem('lsGroupSection', result[4]);
localStorage.setItem('lsGroupNAS', result[5]);
localStorage.setItem('lsNextView', result[6]);
if(result[6] == "1"){
window.location.assign("SelectPerson.html");
}else{
$('#ajaxGetUserServletResponse').text(result[6]);
$("#accountName").focus();
}
window.location.assign("SelectPerson.html");
})