我尝试从 JSP(I-Frame)调用 Spring MVC Controller ,并在浏览器上收到以下错误
400 Bad Request,The request sent by the client was syntactically incorrect ()
这是我向服务器发送请求的 JQuery 代码
jQuery("#login").live('click', function(e) {
e.preventDefault();
jQuery.ajax({
url: "https://localhost:9002/myApp/springSecurity/login.json",
xhrFields: {
withCredentials: true
},
type: "POST",
crossDomain:true,
data: jQuery("#loginForm").serialize(),
contentType: "json",
success: function(data, status) {
alert(data);
alert(status);
if (data.loggedIn) {
// location.href = getHost() + '${ctx}/users';
//login_pannel
} else {
loginFailed(data);
}
},
error: loginFailed
});
});
这是我的 Controller 代码
@Controller
@RequestMapping("springSecurity/login.json")
public class SpringSecurityLoginController
{
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public SpringSecurityLoginStatus login(@RequestParam("j_username") final String username,
@RequestParam("j_password") final String password, final HttpServletRequest request, final HttpServletResponse response)
{
LOG.info("Starting login process");
return springSecurityLoginService.login(username, password, request, response);
}
}
点击提交按钮时,我在服务器上没有收到任何错误,但在查看浏览器控制台输出时,它向我显示以下错误
"NetworkError: 400 Bad Request - https://localhost:9002/myApp/springSecurity/login.json"
这是来自 Mozilla 响应 header 的错误信息
the request sent by the client was syntactically incorrect ().
我不确定是什么导致了这种行为。 另外,我使用 Spring security 来处理身份验证和授权。
编辑
我进行了更多调试,发现当我检查 Controller 中的 j_username
和 j_password
值时,表单值没有提交到我的 Controller ,如下所示空。
我什至尝试了request.getParameter("param name");
但仍然相同的值作为null
最佳答案
在 jQuery 中,尝试将“contentType”更改为“dataType”( more information here )。
关于jquery - 客户端发送的请求在语法上不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11049783/