与 Jersey & 一起发布jQuery
我在使用以下方式发布到 Jersey 时遇到问题...
var details = {username: uname, password: pword};
$.post('login',function(details){
console.log("sent: "+details);
});
我的登录页面中的正确功能正在运行,但用户名
和密码
均为空。
以下是我如何使用它们...
public void login(@FormParam("username") String uname,
@FormParam("password") String pword){
System.out.println("username = " + uname + "password = " + pword);
}
我意识到我发布的不是表单,但 @FormParam
是我能看到的唯一适合这里的 @
。
编辑
好的,我现在有以下内容...
正在发布的帖子
var details = {'username': username, 'password': password};
$.POST('login', details,function(result){
console.log(result);
},"json");
接收方法
@POST
public Viewable HeyStaksLogin(LoginInfo loginI){
String uname = loginI.username;
String pword = loginI.password;
System.out.println("username = ");
}
loginInfo
类
@XmlRootElement
public class LoginInfo {
public String username;
public String password;
}
I'm having an issue POSTing to Jersey using the following...
var details = {username: uname, password: pword};
$.post('login',function(details){
console.log("sent: "+details);
});
The correct function in my login page is being ran but both username
and password
are null.
The following is how I consume them...
public void login(@FormParam("username") String uname,
@FormParam("password") String pword){
System.out.println("username = " + uname + "password = " + pword);
}
I realise what I POST isn't a form but @FormParam
is the only @
I can see that would fit here.
EDIT
Okay, I've got the following right now...
the POST being made
var details = {'username': username, 'password': password};
$.POST('login', details,function(result){
console.log(result);
},"json");
The receiving method
@POST
public Viewable HeyStaksLogin(LoginInfo loginI){
String uname = loginI.username;
String pword = loginI.password;
System.out.println("username = ");
}
The loginInfo
class
@XmlRootElement
public class LoginInfo {
public String username;
public String password;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
数据作为
$.post()
函数(第一个是 url,第三个是成功回调),如下所示:The data is sent as the second argument of the
$.post()
function (the first being the url and the third being the success callback), like this:@FormParam 在这种情况下不可用,因为您发布的是 JSON。以下是您可以执行的操作的示例:
1) 将 jersey-json.jar 添加到项目依赖项
2) 定义以下 bean 来表示消息正文:
3) 修改登录方法,如下所示:
@FormParam is not usable in this case, as what you are posting is JSON. Here is an example of what you could do:
1) add jersey-json.jar to your project dependencies
2) define the following bean to represent the body of the message:
3) modify the login method as follows:
尝试为您的服务器端方法添加 @Consumes(MediaType.APPLICATION_FORM_URLENCODED) 注释
try @Consumes(MediaType.APPLICATION_FORM_URLENCODED) annotation for your serverside method