与 Jersey & 一起发布jQuery

发布于 2024-12-13 07:48:19 字数 1218 浏览 0 评论 0原文

我在使用以下方式发布到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把时间冻结 2024-12-20 07:48:20

数据作为 $.post() 函数(第一个是 url,第三个是成功回调),如下所示:

var details = { username: username, password: password };
$.post('login', details, function(result) {
    console.log(result);
});

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:

var details = { username: username, password: password };
$.post('login', details, function(result) {
    console.log(result);
});
十年九夏 2024-12-20 07:48:20

@FormParam 在这种情况下不可用,因为您发布的是 JSON。以下是您可以执行的操作的示例:

1) 将 jersey-json.jar 添加到项目依赖项

2) 定义以下 bean 来表示消息正文:

@XmlRootElement
public class LoginInfo {
    public String username;
    public String password;
}

3) 修改登录方法,如下所示:

public void login(LoginInfo li) {
    System.out.println("username = " + li.username + "; password = " + li.password);
}

@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:

@XmlRootElement
public class LoginInfo {
    public String username;
    public String password;
}

3) modify the login method as follows:

public void login(LoginInfo li) {
    System.out.println("username = " + li.username + "; password = " + li.password);
}
菩提树下叶撕阳。 2024-12-20 07:48:20

尝试为您的服务器端方法添加 @Consumes(MediaType.APPLICATION_FORM_URLENCODED) 注释

try @Consumes(MediaType.APPLICATION_FORM_URLENCODED) annotation for your serverside method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文