无法为MVC控制台执行AJAX调用

时间:2019-01-08 11:44:50

标签: ajax spring-mvc

我只想对我的Spring MVC控制器使用ajax调用,但是Ajax调用不影响控制器的方法。我做错了吗?

我的Jsp代码[ModifyUser.jsp]:

<form name="testForm" >
<table>
<tr><th>User ID</th><td><input id="user_id"  type="text" value="AD001" 
readonly="readonly"> </td></tr>
<tr><th>Name</th><td><input id="user_name"  type="text" value="ABC SBC "> 
</td></tr>
<tr><th>E-mail</th><td><input id="user_email" type="text" 
value="asd@xyz.com"> </td></tr>
</table>
<form>
<script>
function deleteUser()
{ 
    alert("going to delete user");
 $.ajax({
    type : "GET",
    url : "${pageContext.request.contextPath}/deleteUserReq",
    data : {
    "usrId" : ${user_id}
    },
    success: function(data){
    //response from controller
         alert(data);
    }
    });
}
</script>

我的控制器代码:

@RequestMapping("deleteUserReq")
@ResponseBody
public ModelAndView inactiveUserReq(@RequestParam HttpServletRequest 
request, HttpServletResponse response, Model model) {
System.out.println("===== going to delete user ===== ");
/*String userChk=reportDAOImpl.inactiveUser(userID);*/
String userChk="success";
System.out.println("  ===  "+userChk);
return new ModelAndView("ModifyUser","responsetxt",userChk);

}

2 个答案:

答案 0 :(得分:0)

  

您不应返回模型并使用响应主体(无论是字符串)进行查看   或可以由jackson(Map,ArrayList)转换为json的东西

@RequestMapping("deleteUserReq")
@ResponseBody
public String inactiveUserReq(@RequestParam HttpServletRequest 
request, HttpServletResponse response, Model model) {
System.out.println("===== going to delete user ===== ");
/*String userChk=reportDAOImpl.inactiveUser(userID);*/
String userChk="success";
System.out.println("  ===  "+userChk);
//return new ModelAndView("ModifyUser","responsetxt",userChk);
//return string or json
return "success";
}

我希望对您有帮助

答案 1 :(得分:0)

URl没有调用deleteUserReq控制器

http://localhost:8085/CdfPortal/userModify?usrId=BU941003 [Actual result]
http://localhost:8085/CdfPortal/deleteUserReq?usrId=BU941003 [Required]