gpt4 book ai didi

javascript - 为什么我的 JSON.stringify() 不能与 localstorage.setItem() 一起使用?

转载 作者:行者123 更新时间:2023-11-28 08:18:49 24 4
gpt4 key购买 nike

我从页面上的表单中提取了一个对象。我把这个对象插入一个数组中。

var postObj = {title:$('input').val(),body:$('textarea').val()}
postArray.push(postObj);
localstorage.setItem("posts", JSON.stringify(postArray));

但是,我刚刚收到Uncaught ReferenceError: Localstorage is not Defined

在控制台中,我从测试中得到这些响应。

postObj
Object {title: "title", body: "dog"}
postArray
[
Object
body: "dog"
title: "title"
__proto__: Object
]
JSON.stringify(postArray);
"[{"title":"title","body":"dog"}]"

我发现类似问题的大多数回复都说使用 JSON.stringify(array)。我不知道为什么当我使用 JSON.stringify 作为 .setItem 的数据参数时它不起作用。

最佳答案

看起来你有一个错字,应该是:

window.localStorage//注意驼峰名称

关于javascript - 为什么我的 JSON.stringify() 不能与 localstorage.setItem() 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23257525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com