我正在尝试为我的网站创建一个简单的登录功能。我正在使用 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")
});
});
最佳答案
从外观上看,您应该使用 @FormDataParam
而不是 @FormParam
。
我在这里找到它:https://groups.google.com/forum/#!topic/dropwizard-user/bYyG-Pvk29Y ,但还是不太明白其中的道理。
关于javascript - 当请求实体的内容类型不是 application/x-www-form-urlencoded 时使用@FormParam],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44587982/