jquery - 客户端发送的请求在语法上不正确

标签 jquery spring spring-mvc spring-security

我尝试从 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_usernamej_password 值时,表单值没有提交到我的 Controller ,如下所示空。

我什至尝试了request.getParameter("param name");但仍然相同的值作为null

最佳答案

在 jQuery 中,尝试将“contentType”更改为“dataType”( more information here )。

关于jquery - 客户端发送的请求在语法上不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11049783/

相关文章:

Javascript 警报不止一个

spring - 如果 Spring boot REST Api 中存在额外参数,如何拒绝请求并发送自定义消息

java - Spring通用服务

javascript - 在 spring mvc 的某个时间后关闭 jquery ui 弹出窗口后必须重定向 html 页面

javascript - jQuery - 使粘性页脚停止在页脚顶部,并在向上滚动时再次变得粘性

javascript - 如何检查多行单元格是否已填充

javascript - Accordion 遏制无法正常工作

java.lang.IllegalStateException : Cannot load configuration class : com. 项目.config

java - Oauth 2.0 - 单个资源服务器但多个客户端应用程序

java - Spring通用抽象 Controller 与接口(interface)GET/DELETE方法映射