我正在嘗試呈現對象數組(高熱點點)。數據應該沒問題,但是當我嘗試渲染時,我得到[object Object]
而不是數據本身。express.js - 呈現完整數據
JSON.stringify()
不適合HTML。
util.inspect
,也沒有,並添加數據。
toString()
給我跟渲染一樣。
我不知道還有什麼可以嘗試的,我試圖發送的是一張高圖表的數據。
最少例如:
app.js:
var express = require('express'),
app = express();
app.listen(8080);
app.get('/', function (req, res) {
var view = 'test.ejs',
theme = [{name: '1', y: 5}, {name: '2', y: 2}];
res.render(view, {theme: theme});
console.log('ok');
});
theme_days.ejs:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<script type="text/javascript">
<%= theme %>
</script>
</body>
</html>
結果(彷彿,toString()
):
[object Object],[object Object]
結果與JSON.stringify()
:
[{"name":"1","y":5},{"name":"2","y":2}]
結果與util.inspect
:
[ { name: '1', y: 5 }, { name: '2', y: 2 } ]
編輯: 我現在明白了,有何happenning是'
被轉義爲HTML,是有辦法阻止?
的eacaping happends在模板水平看到這對於溶液http://stackoverflow.com/questions/ 8547131/how-to-include-html-code-in-a-view – Max 2014-08-28 09:58:36
@Max:Urg,和JSON.stringify不斷解決問題... – DrakaSAN 2014-08-28 10:15:06