admin管理员组文章数量:1429953
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 acplish 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.
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])
}
});
}
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 acplish 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.
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])
}
});
}
Share
Improve this question
asked Apr 15, 2013 at 23:13
RobRob
11.5k10 gold badges40 silver badges56 bronze badges
1
- 1 Because DOM Storage does store them as strings (see second Note). Thus, to store objects, they need to be serializable to store, and deserializable to retrieve. – Joseph Commented Apr 15, 2013 at 23:17
1 Answer
Reset to default 0You can only store strings in the localStorage
, however you could create an abstraction of the serialization/deserialization process in a reusable function.
Have a look at Storing Objects in HTML5 localStorage
Also, it's remended to use the setItem
and getItem
methods.
本文标签: javascriptUse localStorage to hold ajax response and then use as objectStack Overflow
版权声明:本文标题:javascript - Use localStorage to hold ajax response and then use as object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745554554a2663104.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论