Java 在ajax请求时找不到参数
我想使用AJAX请求将表单中的数据添加到DB中。调用发生更改的页面的控制器接受“事件”参数Java 在ajax请求时找不到参数,java,ajax,spring-mvc,Java,Ajax,Spring Mvc,我想使用AJAX请求将表单中的数据添加到DB中。调用发生更改的页面的控制器接受“事件”参数 @RequestMapping(value = "/event") public ModelAndView event(HttpServletRequest request, HttpSession session) { ModelAndView modelAndView = new ModelAndView(); SimpleUser simpleUser = simpleUserSer
@RequestMapping(value = "/event")
public ModelAndView event(HttpServletRequest request, HttpSession session)
{
ModelAndView modelAndView = new ModelAndView();
SimpleUser simpleUser = simpleUserService.findById((Integer) session.getAttribute("userId"));
Profile profile = simpleUser.getProfile();
modelAndView.addObject("nameOfEventOwner", profile.getName());
modelAndView.addObject("surnameOfEventOwner", profile.getSecondName());
int id = Integer.parseInt(request.getParameter("event"));
System.out.println(id);
modelAndView.addObject("event", eventService.findById(id));
modelAndView.setViewName("eventpage");
return modelAndView;
}
页面上有一个表单,我希望从中输入db的数据(稍后在页面上动态输入)
函数调用(){
var comment=$(“#comment”).val();
风险值比率=$(“#比率”).val();
$.ajax({
url:“/addcomment”,
方法:“GET”,
数据:{
评论:评论,,
比率:比率,
idEvent:${event.getId()},
idAuthor:${sessionScope.simpleUser.getId()}
},
数据类型:“html”,
成功:功能(结果){
警告(“添加”);
}
});
};
标题
所有者:${NameOfVentowner}${NameOfVentowner}
${event.getNameOfEvent()}${event.getDateOfEvent()}
${event.getDescription()}
${event.getTag()}
评论:
评级:
添加评论
我想通过AJAX将这些值传递给控制器。但是当我传递这些值时,我的页面被重新加载,我被NumberFormatExeption错误打断,因为'event'参数没有初始化。如何解决此问题?将按钮类型更改为“按钮” 当类型为“提交”时,操作将尝试提交表单,并重新加载页面 使用“按钮”类型,只需将操作委托给
call()
函数即可
添加注释
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: Yura
Date: 10.05.2017
Time: 15:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<script src="<c:url value="/resources/jquery-3.2.1.min.js" />"></script>
<script>
function call() {
var comment = $("#comment").val();
var rate = $("#rate").val();
$.ajax({
url : "/addcomment",
method : 'GET',
data : {
comment : comment,
rate : rate,
idEvent: ${event.getId()},
idAuthor: ${sessionScope.simpleUser.getId()}
},
dataType: "html",
success : function(results){
alert("add");
}
});
};
</script>
<title>Title</title>
</head>
<body>
owner: ${nameOfEventOwner} ${surnameOfEventOwner}
<br>${event.getNameOfEvent()} <pre> ${event.getDateOfEvent()}}</pre>
<br> ${event.getDescription()}
<br> ${event.getTag()}
<br>
<form>
Commentary: <input type="text" id="comment" placeholder="text">
Rating: <input type="text" id="rate" placeholder="rating">
<button type="submit" id="button" onclick="call()">Add commentary</button>
</form>
</body>
</html>
<button type="button" id="button" onclick="call()">Add commentary</button>