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

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条回答 默认 最新

  • 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";
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证