客户端发送的请求语法错误

Posted

技术标签:

【中文标题】客户端发送的请求语法错误【英文标题】:The request sent by the client was syntactically incorrect 【发布时间】:2012-06-18 11:18:21 【问题描述】:

我正在尝试从 JSP(I-Frame)调用我的 Spring MVC 控制器并在浏览器上收到以下错误

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
@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 响应标头的错误信息

the request sent by the client was syntactically incorrect ().

我不确定是什么导致了这种行为。 此外,我正在使用 Spring 安全性来处理身份验证和授权。

编辑

当我检查控制器中j_usernamej_password 的值是否为空时,我进行了更多调试,发现表单值没有提交给我的控制器。

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

【问题讨论】:

您能否提供如 Firebug 所示的完整请求(请求 + 数据)? 请记住,您也可以为答案投票。 ;-) @Stealth:同意并完成。感谢您的快速回答 【参考方案1】:

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

【讨论】:

以上是关于客户端发送的请求语法错误的主要内容,如果未能解决你的问题,请参考以下文章

验证表单“发送的请求在语法上不正确”时出现错误 400

使用用户输入发送套接字GET请求的语法是什么

通过 Python API 客户端将经过验证的查询发送到 BigQuery 时出现语法错误

逻辑错误的 HTTP 400(错误请求),不是格式错误的请求语法

Flask之勾子,错误捕获以及模板语法

使用 JDBC 的 Xquery -- 错误的 SQL 语法