【发布时间】:2020-10-22 08:18:57
【问题描述】:
我想使用 AJAX 将 URI 值发送到数据库,并且请求是用 @Controller 编写的。但是当我点击提交时,数据库中没有任何内容。 例如我有地址:
http://localhost:8080/post/view/2
如上所述,我希望每次单击提交时都将值 2 保存到我的数据库中。我试过了,但什么都没有
@控制器
@RequestMapping(value="post/view/{id}", method= RequestMethod.POST, produces = "apllication/json")
public @ResponseBody Comment newComment (@RequestParam(name="id") Long id) {
Comment comment = new Comment();
Post postView = postService.findById(id);
comment.setPoster(postView);
commentService.create(comment);
return comment;
}
还有我的代码 HTML
view.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head >
<script type="text/javascript" language="javascript"
src="https://www.technicalkeeda.com/js/javascripts/plugin/jquery.js"></script>
<script type="text/javascript"
src="https://www.technicalkeeda.com/js/javascripts/plugin/json2.js">
</script>
<title th:text="${postView.title}">View Post</title>
<script >
function madeAjaxCall(){
$.ajax({
type: "post",
url: "http://localhost:3313/post/view/{id}"
}
</script>
</head>
<body>
<div>
<form method="post" >
<main id="posts">
<article>
<h2 class="title" th:text="${postView.title}">Post Title</h2>
<div class="date">
<i>Posted on</i>
<span th:if="${postView.author}" th:remove="tag">
<i>by</i>
<span th:text="${ postView.author.lastName}">Svetlin Nakov</span>
</span>
</div>
</article>
<input type="button" value="Ajax Submit" onclick="madeAjaxCall();"/>
</main>
</form>
</div>
</body>
</html>
我认为我在 AJAX 或 @Controller 上错了或遗漏了一些东西。谢谢
【问题讨论】:
-
你有没有设置断点,一步步跑过控制器?