Spring MVC Ajax 400错误请求 [英] Spring MVC Ajax 400 bad request
本文介绍了Spring MVC Ajax 400错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我触发Ajax请求时,我收到了"400:错误请求",这是一个右上角"登录.我不知道为什么在萤火虫中,我可以看到请求url正确,但是返回了"400:Bad Request".我无法调试,因为似乎客户端尚未将任何内容成功发送回控制器.我猜在我的代码中使用ajax是一个错误.
I got a "400 : Bad Request" when I trigger the Ajax request, it's a 'up-right-corner' login. I don't know why. In firebug, I can see that the request url is correct, but it returns the "400 : Bad Request". I can't debug because it seems the client hasn't sent anything successfully back to the controller. I guess there's a mistake of using ajax in my code.
控制器:
@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>
FireBug标头:
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请求网址正确.
Firebug request URL is correct.
推荐答案
尝试一下:
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public String performLogin(@RequestBody String json, HttpServletRequest request, HttpServletResponse response) {
...
}
这篇关于Spring MVC Ajax 400错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文