首页 > 解决方案 > 如何从#authentication.getPrincipal() 获取多个数据。在Javascript中

问题描述

我获取有关用户的信息并使用 AJAX 将其发送到服务器。

这项工作很好:

<script>
       /*<![CDATA[*/  
            data.orderId = [[${theOrder.id}]]; 
            data.UserId = [[${#authentication.getPrincipal().getId()}]]; /*]]>*/
</script>

当我再添加一个时:

/*<![CDATA[*/ data.fromUserName = [[${#authentication.getPrincipal().getName()}]]; /*]]>*/

它破坏了代码并在浏览器控制台中抛出错误:Uncaught SyntaxError: Unexpected identifier。我在 thymeleaf 上检查 .getName() 并且它有效。

标签: javascriptauthenticationsyntax-errorthymeleaf

解决方案


我不知道为什么它不适用于一对一的变量。但在 Thymeleaf 3 中,这种结构适用于许多变量:

<script th:inline="javascript">
    var username = [[${session.user.name}]];
</script>

推荐阅读