iam使用Spring mvc和iam尝试进行ajax调用,但该调用未在controller方法上进行。 我粘贴了以下代码。有人可以在这方面帮助我吗?
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript">
$( function() {
$( ".datepicker" ).datepicker();
} );
</script>
<script>
function submitLeave(empid){
console.log("hi iam in submit leave method");
alert("herreeee");
var x = document.getElementById("startDate").value;
console.log("x= "+x);
alert("x=="+x);
console.log("empid "+empid);
alert("empid=="+empid);
$.ajax({
type : 'GET',
url : 'testLeave',
data: { 'startDate':x,
'empid':empid
},
success: function(response){
alert(response);
},
error: function(){
alert("error");
}
});
}
</script>
</head>
<body style="background-color:powderblue" >
Welcome ${pageContext.request.userPrincipal.name}
Hi ${employee.name}, your employee id is ${employee.empid} <br><br><br><br>
<i>inside testwelcome.jsp file</i>
<br><br>
<h1>Apply your leave here</h1>
<form:form method="GET" onsubmit="submitLeave('${employee.empid}')" >
<label for="datepicker"> start date</label>
<input type="text" name="startDate" placeholder="start date" class="datepicker" id="startDate"/> <br>
<label for="datepicker">end date</label>
<input type="text" name="endDate" placeholder="end date" class="datepicker" id="endDate"/> <br>
<label > Reason</label>
<input type="text" name="reason" placeholder="Reason" id="reason"/> <br>
Type of leave
<select name="leaveType" id="leaveType">
<option value="casual">Casual</option>
<option value="sick">Sick</option>
</select>
<input type="submit" value="submit"/>
</form:form>
<br><br>
<form method="POST" action="/logout">
<input type="submit" value="logout">
<%-- <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> --%>
</form>
</body>
</html>
控制器类如下:
@GetMapping("/testLeave")
public @ResponseBody String testLeave (@RequestParam String startDate, @RequestParam String empid) {
System.out.println("inside test leave "+startDate+"$$$$$"+empid);
return "success";
}
我尝试在ajax的url字段中附加.html .jsp,但我没有解决我的问题...有人可以帮助我跟踪问题
答案 0 :(得分:0)
我解决了问题。...问题是以下几行
即使我用employee.empid调用ajax,这样做也不会生效,因为整个表单已提交,而employee.empid没有通过。.
我将onsubmit从标记转移到标记