[英]Can't understand the error with JSON.parse
I'm working on a todoList and I'm understanding the process of localstorage, so i tried adding default items in my todoList我正在处理一个 todoList 并且我了解 localstorage 的过程,所以我尝试在我的 todoList 中添加默认项
var bla = "[{text: 't1', key: 1602048390632}, {text: 't2', key: 1602048392604}, {text: 't3', key: 1602048393953}]";
this.state = {
items: JSON.parse(bla)
};
But here It shows a cross origin that react doesn't have access to show.但是这里它显示了一个交叉原点,react 无权显示。 The output I need is t1, t2, t3.我需要的输出是 t1、t2、t3。 The CORS error CORS 错误
ReactDOM.render(
<div>
<TodoList/>
</div>,
You cannot use JSON.parse
for bal
variable because the key is not covered with double quotes("").您不能将JSON.parse
用于bal
变量,因为该键没有被双引号 ("") 覆盖。
In this case, you can use eval() function.在这种情况下,您可以使用eval()函数。
var bla = "[{text: 't1', key: 1602048390632}, {text: 't2', key: 1602048392604}, {text: 't3', key: 1602048393953}]"; const state = { items: eval(bla) }; console.log(state);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.