我对Spring启动的了解非常少。 我用HTML创建了一个表,当我点击它时,一个事件被发送到一个Javascript函数,我尝试捕获单元格的Id(字符串)并将其发送到控制器。我尝试过很多来自互联网的例子,但没有一个适合我,有人可以帮我吗? 通过鼠标单击正确捕获id,但我在发送时遇到问题。
这是我的JSP
<c:forEach items= "${rooms2}" var="m" >
<tr>
<td style="background-color:OldLace">${m.day }</td>
<c:choose>
<c:when test="${m.hour1confirm==false}">
<td style="background-color:green" id="${m.hour1}" onclick=
"content(this)" >${m.hour1 }</td>
...
<script >
// <script type="text/javascript">
function content(elem)
{
// $(document).ready(function()
// {
// // PREPARE FORM DATA
// var idt =elem.id;
// console.log(idt);
// // DO POST
// $.post({
// type : "POST",
// url :"reservation2",
// // contentType: "application/json",
// data : JSON.stringify(idt),
// dataType : 'json',
// })
// });
var idt =elem.id;
console.log(idt);
$.ajax({
url:"reservation2",
type: 'POST',
data: idt,
dataType: "text",
contentType: false,
mimeType: false,
success: function(response){
console.log(data);
return false;
}
});
}
</script>
这是控制器
.
.
.
// @RequestMapping(value = "/reservation2", method = RequestMethod.POST)
// public String reservation2(@ModelAttribute("idt") String idt , Model model) {
// return "Reservation2";}
@RequestMapping(value = "/reservation2", method = RequestMethod.POST)
public @ResponseBody
String Submit(@RequestParam("idt") String idt) {
// your logic here
return "reservation2";
}
@RequestMapping(value = "/reservation2", method = RequestMethod.GET)
public String reservation(Model model) {
List<Room> rooms2= new ArrayList<>();
System.out.println(rooms2);
rooms2= x.findAll();
model.addAttribute("rooms2", rooms2);
return "reservation2";
...
当我跑步时,我在控制台中收到此错误:
POST http://localhost:8080/reservation2 403 ()
答案 0 :(得分:0)
尝试将您的请求更改为此类
$.ajax({
url: "/reservation2", //added '/' in the beginning
type: 'POST',
data: idt,
dataType: "text",
contentType: "text/plain", //change
//mimeType: false, //remove
success: function(response) {
console.log(data);
return false;
}
});
和您的控制器
@RequestMapping(value = "/reservation2", method = RequestMethod.POST)
public String reservation2(@RequestBody String idt)
{
// your logic here
return "reservation2";
}
答案 1 :(得分:0)
我相信你一直没有发送正确的参数。当您执行ajax post请求时,在字段数据中是否可以指定要发送的参数的名称?
你能试试这个
吗?$.ajax({
url:"reservation2",
type: 'POST',
data: {"idt": idt},
dataType: "json",
contentType: "application/json",
success: function(response){
console.log(data);
return false;
}
});