2

当我触发 Ajax 请求时,我收到“400:错误请求”,这是一个“右上角”登录。我不知道为什么。在 firebug 中,我可以看到请求 url 是正确的,但它返回“400:错误请求”。我无法调试,因为客户端似乎没有成功将任何内容发送回控制器。我想在我的代码中使用 ajax 是一个错误。

控制器:

@Controller
@RequestMapping("/login")
public class AjaxLoginController {
@Autowired
SecurityContextRepository repository;

@Autowired
RememberMeServices rememberMeServices;

@RequestMapping(method = RequestMethod.GET)
public void login() {
}

@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public String performLogin(@RequestParam("j_username") String username,
        @RequestParam("j_password") String password, HttpServletRequest request, HttpServletResponse response) {
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
    try {
        Authentication authentication = authenticationManager.authenticate(token);
        SecurityContextHolder.getContext().setAuthentication(authentication);
        repository.saveContext(SecurityContextHolder.getContext(), request, response);
        rememberMeServices.loginSuccess(request, response, authentication);
        return "{\"status\": true}";
    } catch (BadCredentialsException e) {
        return "{\"status\": false, \"error\": \"Bad Credentials\"}";
    }
}

JSP:

<script type="text/javascript">
function doLogin() {
    var url = "${login_url}";
    var username = $("#j_username").val();
    var password = $("#j_password").val();
    $.ajax({
        url : url,
        type : "POST",
        cache : false,
        async : true,
        contentType : "application/json; charset=UTF-8",
        data : JSON.stringify({
            "j_username" : username,
            "j_password" : password
        }),
        datatype : "json",
        success : function(data) {
            alert("YES");
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.status + " : " + errorThrown);
        }
    });
}

<form id="login_div" class="navbar-form navbar-right" method="post"
onsubmit="return false;">
            <div class="form-group">
                <input id="j_username" class="form-control" type="text"
                    placeholder="Username">
            </div>
            <div class="form-group">
                <input id="j_password" class="form-control" type="password"
                    placeholder="Password">
            </div>
            <button class="btn btn-success" type="submit" onclick="doLogin();">Sign
                in</button>
        </form>

萤火虫标题:

Accept  */*
Accept-Encoding gzip, deflate
Accept-Language zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Cache-Control   no-cache
Connection  keep-alive
Content-Length  42
Content-Type    application/json; charset=UTF-8
Cookie  JSESSIONID=AEBEB562DD68FF2023C03D54245B0C4A
Host    localhost:8099
Pragma  no-cache
Referer http://localhost:8099/springmvc/dishes/not_found
User-Agent  Mozilla/5.0 (Windows NT 6.2; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0
X-Requested-With    XMLHttpRequest

Firebug 请求 URL 正确。

4

2 回答 2

4

尝试这个:

@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public String performLogin(@RequestBody String json, HttpServletRequest request, HttpServletResponse response) {
...
}
于 2013-09-17T02:08:06.073 回答
-1

尝试使用@ModelAttribute. 这将是工作。

于 2018-08-08T13:24:14.937 回答