繁体   English   中英

无法理解 JSON.parse 的错误

[英]Can't understand the error with JSON.parse

我正在处理一个 todoList 并且我了解 localstorage 的过程,所以我尝试在我的 todoList 中添加默认项

var bla = "[{text: 't1', key: 1602048390632}, {text: 't2', key: 1602048392604}, {text: 't3', key: 1602048393953}]"; 
   
   this.state = { 
    items:  JSON.parse(bla)
   };

但是这里它显示了一个交叉原点,react 无权显示。 我需要的输出是 t1、t2、t3。 CORS 错误

 ReactDOM.render(
   <div>
    <TodoList/>
      </div>,

您不能将JSON.parse用于bal变量,因为该键没有被双引号 ("") 覆盖。

在这种情况下,您可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM