【问题标题】:ajax call not going in spring mvcajax调用不会在spring mvc中进行
【发布时间】:2019-04-26 08:09:05
【问题描述】:

iam 使用 Spring mvc 并且 iam 尝试进行 ajax 调用,但调用未落在控制器方法上。 我已经粘贴了下面的代码。有人可以在这方面帮助我吗?

<%@ 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 但我没有解决我的问题...有人可以帮我追踪问题

【问题讨论】:

  • 您确定您的应用程序在正确的 URL 上执行 GET 请求吗?要在 Chrome/Firefox 中验证它,请打开浏览器,按 F12 打开开发者工具,选择 Network 选项卡并执行操作以检查应用程序调用的 URL。

标签: ajax spring spring-mvc


【解决方案1】:

我解决了问题....问题出在下面一行

即使我用employee.empid 调用ajax 也没有任何效果,因为提交了整个表单并且没有通过employee.empid..

我将 onsubmit 从标签转移到标签

【讨论】:

    猜你喜欢
    • 2014-11-20
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多