1

我有一个变量$data['tree'],它使用模型中的方法从数据库中获取一个行数组get_tree($id)

在我看来,我使用的 js

var dbTree = JSON.parse(<?php echo $tree; ?>);

当我加载页面时,我UncaughtSyntax Error: Unexpected token o在 chrome 和 Firefox 中得到Syntax Error: Unexpected character.

所以当我用 chrome 检查脚本元素时,js 看起来像

var dbTree = JSON.parse({"id":"2","name":"sean","userId":"51fbd3f3a8f2ba1b5b000002","accountId":"51fbd3fca8f2ba1b5b000003","createdAt":"2013-08-02 16:09:34","numRuns":null,"contactExport":"","updatedAt":"2013-08-02 20:15:14","deployed":"1","template":"0","conversation_type":"Conversation"});

我看不出有什么问题可以帮助我。

4

2 回答 2

2

JSON.parse 应该与字符串一起使用,而不是对象。

或者你甚至不需要做任何事情。它已经是一个对象。

就像...

var dbTree = <?php echo $tree; ?>;
于 2013-08-13T20:18:52.617 回答
2
var dbTree = {"id":"2","name":"sean","userId":"51fbd3f3a8f2ba1b5b000002","accountId":"51fbd3fca8f2ba1b5b000003","createdAt":"2013-08-02 16:09:34","numRuns":null,"contactExport":"","updatedAt":"2013-08-02 20:15:14","deployed":"1","template":"0","conversation_type":"Conversation"};
// Test it works
console.log(dbTree);

只需摆脱 parse 功能。

于 2013-08-13T20:19:53.370 回答