Hi have the following Ajax request that gets JSON from the server and stores the json in localStorage for which I can use in my application. It seems like my code to accomplish this is not efficient at all, but I cant get it to work any other way. I have tried storing as an object, but it seems to want to be stored as a string.
您有以下Ajax请求从服务器获取JSON并将json存储在localStorage中,我可以在我的应用程序中使用它。似乎我的代码实现这一点根本没有效率,但我不能让它以任何其他方式工作。我曾尝试将其存储为对象,但它似乎希望存储为字符串。
Is there a better way to do this?
有一个更好的方法吗?
//GET Users List from Server
function getUsersList() {
$.ajax({
url: "api/ListUsers",
dataType: "json",
success: function (e) {
localStorage.UsersList = (JSON.stringify(e));
var usersList = localStorage.UsersList;
var usersList = $.parseJSON(usersList)
console.log(usersList[0])
}
});
}
1 个解决方案
#1
0
You can only store strings in the localStorage
, however you could create an abstraction of the serialization/deserialization process in a reusable function.
您只能在localStorage中存储字符串,但是您可以在可重用函数中创建序列化/反序列化过程的抽象。
Have a look at Storing Objects in HTML5 localStorage
看一下在HTML5 localStorage中存储对象
Also, it's recommended to use the setItem
and getItem
methods.
此外,建议使用setItem和getItem方法。
#1
0
You can only store strings in the localStorage
, however you could create an abstraction of the serialization/deserialization process in a reusable function.
您只能在localStorage中存储字符串,但是您可以在可重用函数中创建序列化/反序列化过程的抽象。
Have a look at Storing Objects in HTML5 localStorage
看一下在HTML5 localStorage中存储对象
Also, it's recommended to use the setItem
and getItem
methods.
此外,建议使用setItem和getItem方法。