weixin_33713707 2015-03-21 15:43 采纳率: 0%
浏览 10

Ajax成功无法执行

I'm doing a login page with Spring MVC in server and JS & Ajax in client. I don't know what's wrong, the code of the server executes but don't return nothing. login.html

<script type="text/javascript">
    $(function() {
        $(".loguser").click(function() {
            var user = $("#login").val();
            var pass = $("#pass").val();            
            $.ajax({
                method: "POST",
                url : "${prefix}loginUser",
                data : "username=" + user + "&password=" + pass,
                dataType : "json",
                success: function(data){
                    if (data.res == "YES") alert("ok");
                    else alert("NOPE");
                }

            });
        })
    })
</script>
    <table id="userData" class="center">

                    <tr id="usernametr">
                        <th><label for="user">Nombre de usuario: </label></th>
                        <th><input id="login" type="text" name="login" value=""
                            placeholder="Name" required /></th>
                    </tr>
                    <tr>
                        <th><br /></th>
                    </tr>
                    <tr>
                        <th><label for="pass">Clave de usuario: </label></th>
                        <th><input id="pass" type="password" name="pass" value=""
                            placeholder="Password" required /></th>
                    </tr>

                    <tr>                        
                        <th><button class="loguser">Acceder</button></th>
                        <th><input type="button" name="lost"
                            value="He perdido mi clave" /></th>
                    </tr>
                </table>

HomeController.java:

@RequestMapping(value = "/loginUser")
    @ResponseBody
    @Transactional
    public String loginUser(@RequestParam("username") String username,
            @RequestParam("password") String pass, HttpServletRequest request, Model model) {
        logger.info("Trying to log in {} {}", username, pass);
        if (username.length() > 3) {
            logger.info("ok");
            return new String("[\"res\": \"YES\"]");
        } else {
            logger.warn("nope");
            return new String("[\"res\": \"NOPE\"]");
        }
    }

I tryiend returning EntityResponse too but nothing change. Springs console prints the logger info or warn but Firefox's javascript console doesnt.

  • 写回答

1条回答 默认 最新

  • weixin_33701617 2015-03-21 16:03
    关注

    You must specify Requestmethod in controller like

    @RequestMapping(value = "/loginUser", method = RequestMethod.POST)

    You must Return Json Values In { } Braces

    评论

报告相同问题?

悬赏问题

  • ¥15 关于龙芯1b,JTAG停止调试服务
  • ¥20 收一个快手协议下单算法
  • ¥15 神途脚本逐鹿神途,管理自欺欺人
  • ¥15 求一个图片中的成交量选股公式
  • ¥15 已知正方形内随机生成坐标matlab
  • ¥30 关于#python#的问题:我想要的是这79个大特征对于房屋售价的最大的影响前十名(相关搜索:随机森林)
  • ¥15 react native新项目运行时遇到问题
  • ¥15 脚本运行Python文件,报错:error: unrecognized
  • ¥15 使用matlab计算自定义特殊函数的二重积分,改变积分顺序所得的结果不同的问题?
  • ¥15 如何正确在vs2010中初始化map对象