【发布时间】:2019-06-03 18:48:24
【问题描述】:
我正在尝试编写应用程序的前端,但遇到了问题。我一直在尝试使用 AJAX 实现 DELETE 方法,但是根据 Spring,当我运行代码时会发送 GET。
HTML 代码:
<tr th:each="attraction : ${attractions}" th:object="${attraction}">
<td th:text="*{name}"></td>
<td th:text="*{latitude}"></td>
<td th:text="*{city}"></td>
<td><a th:href="|/edit/*{id}|">EDIT</a></td>
<script>
function sendDelete(event) {
xhttp.preventDefault();
xhttp.open("DELETE", this.href);
xhttp.send();
}
</script>
<td><a th:href="|/delete/*{id}|" onclick="sendDelete(event);">DELETE</a></td>
</tr>
弹簧代码:
@DeleteMapping("/delete/{id}")
String delete(@ModelAttribute Attraction attraction) {
attractionService.delete(attraction);
return "redirect:/";
}
我该如何解决这个问题?提前谢谢你。
【问题讨论】:
-
根据spring,发送一个get是什么意思?你是说你的 spring 代码中的 delete 方法永远不会被调用?
-
这与您的问题无关,但您应该在此处使用带有 th:href 的“@{...}”。设置与“/”不同的上下文根时遇到问题。
-
@RobertMoskal 我只是想指出我得到一个白标签错误,但是是的,它不是由 Spring 完成的......
标签: java html ajax spring thymeleaf