qq_42914617
2019-08-22 22:53
采纳率: 25%
浏览 305
已采纳

为什么这里没有实现跳转到/login?

前端

  <body>
    <form action="checkVerify" method="post">
        姓名:<input type="text" name="name" placeholder="姓名"><br/><p>
        密码:<input type="password" name="password" placeholder="密码"><br/>
            <div class="row">
        <div class="col-xs-6 pull_left">
            <div class="form-group">
                <input class="form-control" type="text" id="verify_input" placeholder="请输入验证码" maxlength="4">
            </div>
        </div>
        <div class="col-xs-6 pull_left">
            <a href="javascript:void(0);" title="点击更换验证码">
                <img id="imgVerify" src="/getVerify" alt="更换验证码" height="36" width="170" onclick="getVerify(this);">
            </a>
        </div>
        <input type="button"  onclick="aVerify()" value="提交">
    </div>

    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript">
        function getVerify(obj){
            obj.src ="/getVerify?"+Math.random();
        }

        function aVerify(){
        var value =$("#verify_input").val();
        $.ajax({
            async: false,
            type: 'post',
            url: '/checkVerify',
            dataType: "text",
            data: {
                verifyInput: value
            },
        });
    }
    </script>
    </form>

controller

@RequestMapping("/login")
    public String login(String name,String password){
        User user = userService.login(name, password);
        if(user!=null){
            return "index";
        }else{
            return "redirect:/page";
        }   
    }

        @RequestMapping("/index")
    public String index(Model model){
        return "index"; 
        }

            @RequestMapping(value = "/getVerify")
    public void getVerify(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("image/jpeg");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
            randomValidateCode.getRandcode(request, response);
        } catch (Exception e) {
            logger.error("获取验证码失败", e);
        }
    }

     @RequestMapping(value = "/checkVerify", method = RequestMethod.POST)
        public String checkVerify(@RequestParam(required=false) String verifyInput, HttpSession session, String name, String password) {

         try{
                String inputStr = verifyInput;
                String random = (String) session.getAttribute("RANDOMVALIDATECODEKEY");
                if (inputStr == null) {
                    System.out.println(inputStr);
                    return "redirect:/page";
                }
                if (inputStr.equals(random)) {
                    System.out.println(inputStr);
                    return "redirect:/login";
                } else {
                    System.out.println(inputStr);
                    return "redirect:/page";
                }
            }catch (Exception e){
                logger.error("验证码校验失败", e);
                return "redirect:/page";
            }
        }

控制台

2019-08-22 22:52:06.272  INFO 3540 --- [p-nio-80-exec-2] c.b.s.util.RandomValidateCodeUtil        : 1519
1519
Hibernate: select user0_.id as id1_0_, user0_.age as age2_0_, user0_.info as info3_0_, user0_.name as name4_0_, user0_.password as password5_0_ from user user0_ where user0_.name=? and user0_.password=?

明明条件inputStr.equals(random)成立却不跳转

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 毕小宝 2019-08-23 09:18
    已采纳

    因为你前端是 ajax 提交的请求,而且并没有写回调函数,这个 ajax 请求响应返回的是一个字符串 “redirect:/login”而不是被解析的跳转页面。
    用 ajax 提交的话,应该接收这个跳转 URL ,然后用 window.href.location = url 完成跳转。
    还可以用该 button submit 自动提交,这样后台的跳转就能会自动生效了。

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题