我试图在javascript变量中获取一个json。我在extbase typo3扩展中使用了fluid-templates。在我的操作中,我使用curl加载一些json。这个json是我分配给模板的。在我的模板中,它看起来像这样:
<script type="text/javascript">
var json = {jsonVarFromControllerAction};
</script>在这种情况下,json被解释为html代码。看起来是这样的:
{"content":"testcontent"}在控制器-action中,它是一个正确的json!
{"content": "testcontent"}解决方案是什么?
发布于 2013-01-10 19:54:14
我写了我自己的视频。这个视频接收器只获取json- html_entity_decode并对其进行内容处理。
public function render($json)
{
return html_entity_decode($json);
}它工作得很好,但我问自己,为什么我要写一个帮助程序来获得我自己变量的纯内容?
发布于 2013-01-09 19:31:25
使用<f:format.htmlentitiesDecode> ViewHelper对其进行解码,例如:
<script type="text/javascript">
var json = <f:format.htmlentitiesDecode>{jsonVarFromControllerAction}</f:format.htmlentitiesDecode>;
</script>您可以在typo3/sysext/fluid/Classes/ViewHelpers中浏览所有可用的ViewHelpers
另一种选择是使用AJAX直接从操作中获取PHP格式的JSON (无需将其传递给视图)。如果您希望在不刷新整个页面的情况下获取新数据,则此方法非常有用。
发布于 2017-02-21 18:34:30
<script type="text/javascript">
var title = {v:format.json.encode(value: branch.name)};
//.. do something
</script>https://stackoverflow.com/questions/14234104
复制相似问题