【问题标题】:Spring boot ajax error with resolving template解析模板的Spring Boot ajax错误
【发布时间】:2019-11-29 05:25:51
【问题描述】:

我的 index.html

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    (js and css)
</head>
<body>
    <div>
        <button name="sendPaperByMessage" th:if="${paper.issend} ne 'sended'" th:paperId="${paper.paperid}">sendMessage</button>
    </div>
    <script th:inline="javascript">
         $("button[name='sendPaperByMessage']").click(function () {
            var paperId = $(this).attr("paperId");
            $.ajax({
                  url: "/api/sendMessage",
                  type: "post",
                  data: {"paperId": paperId},
                  success: function (data) {
                        console.log("success");
                  }
            })
      });
    </script>
</body>
</html>

和我的控制器:

@PostMapping("/api/sendMessage")
    public Object sendMessage(@RequestParam("paperId") String paperId){
//         paperService.sendMessage(paperId);
         Map map = new HashMap();
         map.put("Message", "success");
         return map;
    }

我省略了一些代码并做了一个演示。 响应是错误解析模板 [api/sendMessage],模板可能不存在或可能无法被任何已配置的模板解析器访问”

【问题讨论】:

  • 检查this 可能对你有帮助。

标签: ajax spring-boot thymeleaf


【解决方案1】:

默认情况下,控制器返回的值由 spring 解析。

为了防止这种情况并以 json 格式返回您的对象,请在您的方法上方使用 @ResponseBody 或使用 RestController 而不是 Controller。 但请注意,在休息控制器中,每个响应都被认为不是模板,因此不会被解析。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-30
    • 2019-05-02
    • 2014-05-11
    • 1970-01-01
    • 2021-09-25
    • 2020-10-07
    • 1970-01-01
    • 2020-03-14
    相关资源
    最近更新 更多