qq_42914617
qq_42914617
2019-08-23 15:38
采纳率: 25%
浏览 368

login方法得不到前端name和password的值

前端

 <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="tel" 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: "json",
            data: {
                verifyInput: value
            },
            success: function(result){
                if(result){
                    window.location.href = "/login";
                }else{
                    window.location.href = "/page";
                }

            }

        });
    }   
    </script>
    </form>

后台

@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(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,headers = "Accept=application/json")
     @ResponseBody
        public boolean checkVerify(@RequestParam(required=false) String verifyInput, HttpSession session) {

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

这里的login方法得不到前端name和password的值怎么办

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • tiankongzhichenglyf
    tkzc_shark 2019-08-23 15:44
    已采纳

    请求中没有传参,后台怎么接收

            $.ajax({
                async: false,
                type: 'post',
                url: '/login',
                dataType: "json",
                data: {
                    verifyInput: $("#verify_input").val(),
                                    name: $("input[name='name']").val(),
                                    password: $("input[name='password']").val()
                },
                success: function(result){
                    if("success" != result){
                        alert(result);
                    }else{
                        window.location.href = "/page";
                    }
    
                }
    
            });
    
        @RequestMapping(value = "/login", method = RequestMethod.POST, headers = "Accept=application/json")
        @ResponseBody
        public String login(@RequestParam(required = false) String verifyInput, HttpSession session, String name, String password) {
            try {
                String inputStr = verifyInput;
                String random = (String) session.getAttribute("RANDOMVALIDATECODEKEY");
    
                if (inputStr == null) {
                    return "验证码为空";
                }
                if (inputStr.equals(random)) {
                    //用户名密码校验
                    User user = userService.login(name, password);
                    if (user != null) {
                        return "success";
                    } else {
                        return "登录名或密码错误";
                    }
                } else {
                    return "验证码错误";
                }
            } catch (Exception e) {
                logger.error("验证码校验失败", e);
                return "error";
            }
        }
    
    点赞 评论
  • u013254183
    老铁咖啡 2019-08-23 16:00

    你的用户名密码参数都没往后台传呀

    点赞 评论

相关推荐