jquery ajax bad request,jquery - Spring MVC Ajax 400 bad request - Stack Overflow

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:

@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:

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);

}

});

}

οnsubmit="return false;">

placeholder="Username">

placeholder="Password">

Sign

in

FireBug Header:

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 request URL is correct.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值