我有一个项目可以使用json
和ajax
。我有GET
方法返回ingest.html
模板。之后,来自视图的数据转到POST
方法,其中数据保存在数据库中,然后方法返回不同的视图。所以,我得到的数据转到POST
方法,它保存在数据库中,但是post方法没有显示视图,它停留在同一页面上。我做错了什么?
控制器:
@Controller
public class IngestController {
@Autowired
private IngestService ingestService;
@RequestMapping(value = "/ingest")
public String ingestForm(){
return "/resources/html/ingest.html";
}
@RequestMapping(value = "/ingest", method = RequestMethod.POST)
public String createIngest(@RequestBody IngestMedia ingestMedia){
ingestService.createIngest(ingestMedia);
return "/resources/html/result.html";
}
servlet.xml中:
<context:component-scan base-package="com.controllers">
</context:component-scan>
<context:annotation-config></context:annotation-config>
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:resources mapping="/resources/**" location="/resources/" />
的index.html:
<html>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("#button").click(function() {
title = $("#title").val();
mediaType = $("#mediaType").val();
employeeId = $("#employeeId").val();
var ingestMedia = {
"title": title,
"mediaType": mediaType,
"employeeId": employeeId
}
createIngestRecord(ingestMedia);
});
});
function createIngestRecord(ingestMedia) {
$.ajax({
type: "POST",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
url: "/ingest",
data: JSON.stringify(ingestMedia), // Note it is important
success: function(data) {
alert("Done");
}
});
}
</script>
<body>
<h1> Ingest </h1>
Title<br>
<input type="text" id="title"><br> MediaType
<br>
<input type="text" id="mediaType"><br> Employee Id<br>
<input type="text" id="employeeId"><br>
<br>
<input type="button" id="button" value="submit">
</body>
</html>
&#13;