Jquery调用spring控制器的get方法代替post

时间:2012-06-14 05:33:42

标签: javascript jquery spring-mvc

我试图使用POST方法调用我的Spring MVC控制器的post方法,这里是我的控制器的方法

@Controller
@RequestMapping("springSecurity/login.json")
public class SpringSecurityLoginController
{

    @RequestMapping(method = RequestMethod.GET)
        @ResponseBody
        public SpringSecurityLoginStatus getStatus()
        {
            return springSecurityLoginService.getStatus();
        }

        @RequestMapping(method = RequestMethod.POST)
        @ResponseBody
        public SpringSecurityLoginStatus login(@RequestParam("j_username") final String username,
                @RequestParam("j_password") final String password, final HttpServletRequest request, final HttpServletResponse response)
        {

            LOG.info("Starting login process");
            return springSecurityLoginService.login(username, password, request, response);
        }
}

HTML

<form:form action="${request.contextPath}/j_spring_security_check" class="login-form " id="loginForm"  method="post" commandName="loginForm">

<form:input path="j_username" class="text" id="login_id" value="" name ="j_username"/>
<form:password path="j_password" class="text" value="Password" name="j_password" id="j_password"/>
<input type="submit" class="submit" value="LOGIN" id="login" />
</form:form>

这是我的Jquery代码

jQuery("#login").live('click', function(e) {
        e.preventDefault();

        jQuery.ajax({url: getHost() + "${request.contextPath}/springSecurity/login.json",
            type: "POST",
            beforeSend: function(xhr) {
                xhr.withCredentials = true;
            },
            data: jQuery("#loginForm").serialize(),
            success: function(data, status) {
                if (data.loggedIn) {
                   // location.href = getHost() + '${ctx}/users';
                    //login_pannel

                } else {
                    loginFailed(data);
                }
            },
            error: loginFailed
        });
    });

但上面的代码正在调用

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public SpringSecurityLoginStatus getStatus()

而不是

@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public SpringSecurityLoginStatus login(@RequestParam("j_username") final String username,
@RequestParam("j_password") final String password, final HttpServletRequest request, final HttpServletResponse response)

我不确定为什么会出现这种情况

2 个答案:

答案 0 :(得分:0)

@Controller
@RequestMapping("/springSecurity")

public class SpringSecurityLoginController
{

    @RequestMapping(value="/login.json",method = RequestMethod.GET)
        @ResponseBody
        public SpringSecurityLoginStatus getStatus()
        {
            return springSecurityLoginService.getStatus();
        }

        @RequestMapping(value="/login.json",method = RequestMethod.POST)
        @ResponseBody
        public SpringSecurityLoginStatus login(@RequestParam("j_username") final String username,
                @RequestParam("j_password") final String password, final HttpServletRequest request, final HttpServletResponse response)
        {

            LOG.info("Starting login process");
            return springSecurityLoginService.login(username, password, request, response);
        }
}

进行这一小改动并尝试。

答案 1 :(得分:0)

我猜你正在使用专为普通HTTP POST设计的服务器端代码,然后在成功时重定向GET。

如果你想做Ajax(为用户登录做Ajax是一个非常危险的情况,不要掉以轻心),那么你想要返回更合适的东西,比如JSON,而不是重定向。

在Firebug for Firefox中,在“网络”选项卡中,打开302响应并查看响应标头。我打赌你会发现有一个位置标题告诉浏览器重定向。