当请求实体的内容类型不是application / x-www-form-urlencoded]

时间:2017-06-16 11:19:28

标签: javascript java jquery html jersey

我正在尝试为我的网站创建一个简单的登录功能。我使用JAVA作为后端,我试图使用restservices。我的学校给了我一个带有授权的登录系统的例子。不幸的是我收到了这个错误:java.lang.IllegalStateException:当请求实体的内容类型不是application / x-www-form-urlencoded]时,使用@FormParam。

 @POST
 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
 public Response authenticateUser(@FormParam("username") String username,
 @FormParam("password") String password) {

 <form id='inlogForm' enctype="application/x-www-form-urlencoded" >
    <input type='text' placeholder='username' id='username' />
    <input type='text' placeholder='password' id='password' />
    <input type='button' value='login' id='login' />
    </form>
    </header>   
</body>
<script src="https://code.jquery.com/jquery-3.2.0.min.js"></script>
<script>
    $("#login").click(function(event) {
        var data = $("#loginForm").serialize();
        $.post("restservices/authentication", data, function(response) {
            window.sessionStorage.setItem("sessionToken", response);
            $("#loginForm").hide();
        }).fail(function(jqHXR, textStatus, errorThrown) {
            console.log(textStatus);
            console.log(errorThrown);
            alert("Wrong Username/Password")
        });
    });

1 个答案:

答案 0 :(得分:0)

从外观上看,您应该使用@FormDataParam代替@FormParam

我在这里找到了https://groups.google.com/forum/#!topic/dropwizard-user/bYyG-Pvk29Y,但仍然不太了解推理。