Bootstrap

发送请求得不到返回值问题

1.发送请求,浏览器console报如下错误
Failed to load resource: the server responded with a status of 401 ()

在这里插入图片描述

2.发现是后端代码的问题,注释掉上面一行,改为下面返回成功

在这里插入图片描述

public Authentication attemptAuthentication(HttpServletRequest req, HttpServletResponse res) {
        try {
            //将json格式请求体转成JavaBean对象
            SysUser user = new ObjectMapper().readValue(req.getInputStream(), SysUser.class);
            return authenticationManager.authenticate(
                    new UsernamePasswordAuthenticationToken(
                            user.getUsername(),
                            user.getPassword()
                    )
            );
        } catch (Exception e) {
            e.printStackTrace();
            try {
                //如果认证失败,提供自定义json格式异常
                res.setContentType("application/json;charset=utf-8");
//                res.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                res.setStatus(HttpServletResponse.SC_OK);
                PrintWriter out = res.getWriter();
                Result result = new Result();
                result.setSuccess(false);
                result.setMessage("账号或密码错误!");
                result.setCode(401);
                out.write(new ObjectMapper().writeValueAsString(result));
                out.flush();
                out.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            throw new RuntimeException(e);
        }
    }
;