javascript - 如何从#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() 并且它有效。
解决方案
我不知道为什么它不适用于一对一的变量。但在 Thymeleaf 3 中,这种结构适用于许多变量:
<script th:inline="javascript">
var username = [[${session.user.name}]];
</script>
推荐阅读
- java - 简单的 JSON 文件,只需一个数组即可在我的 Android 应用程序上使用
- php - 如何从摘录的开头删除大写文本?
- python - 将一个 Popen 的标准输出重定向到另一个 Popen 标准输入
- javascript - 如何从打字稿访问文档实例
- c++ - 模板函数参数的 C++17 调用结果和静态断言
- java - 在空手道配置中使用 Spring Boot 的配置
- html - Bootstrap - 如何使图像响应某些断点?
- drupal - 是否可以使用 Drupal 8 获得带有 CSS 背景图像的响应式图像
- javascript - 如何将foreignObject添加到d3圆?
- mariadb - 如何迁移 Mirthconnect 以使用 MySQL/MariaDB