Spring通过Ajax进行调用

时间:2017-02-16 13:03:56

标签: jquery ajax spring

我试图在我的jsp中向Ajax调用Spring控制器,但它没有收听请求。 这是我的代码

我的JSP中的Ajax代码:

$.ajax({
    url : "reSendSecurityCode",
    data : {param : reSendSecurityCode, uid : $('#username').val()},            
    success : function(data) {
      //logic   
    },
    type : 'GET'
});

我的控制器中的代码:

@RequestMapping(value = "reSendSecurityCode", method = RequestMethod.GET)
     public @ResponseBody String reSendSecurityCd(@RequestParam("param") String param, 
     @RequestParam("uid") String uid,String msg, HttpServletRequest req, HttpServletResponse res) {
     //inside logic
     return "";
}

2 个答案:

答案 0 :(得分:0)

我很确定您错过了应用的上下文路径。尝试添加绝对路径" /"到你的JS代码和你的控制器

$.ajax({
    url : "/reSendSecurityCode",
    data : {param : reSendSecurityCode, uid : $('#username').val()},            
    success : function(data) {
      //logic   
    },
    type : 'GET'
});


@RequestMapping(value = "/reSendSecurityCode", method = RequestMethod.GET)
public @ResponseBody String reSendSecurityCd(@RequestParam("param") String param, @RequestParam("uid") String uid,String msg, HttpServletRequest req, HttpServletResponse res) {
     //inside logic
     return "";
}

答案 1 :(得分:0)

替换您的网址如下:

$.ajax({
    url : "<c:url value="/reSendSecurityCode"/>",
    data : {param : reSendSecurityCode, uid : $('#username').val()},            
    success : function(data) {
      //logic   
    },
    type : 'GET'
});

您需要在jsp中添加taglib声明。

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>