Jax-RS和Xmlhttp通信 [英] Jax-RS and Xmlhttp Communication

查看:91
本文介绍了Jax-RS和Xmlhttp通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Java JAX-RS的REST服务器和一个HTML页面.

I have a REST Server in Java JAX-RS and an HTML page.

我想通过xmlhttp POST请求从HTML页面发送一个JSON数组,一个用户名和一个accountID,方法是将它们全部设置为一个大字符串,以便可以使用xmthttp.send()方法.

I want to send a JSON array, a username, and an accountID from the HTML page through an xmlhttp POST request by making all of them a single big String so I can use the xmthttp.send() method.

HTML发送代码为:

The HTML sending code is:

function sendData() {   
    var req = createRequest();
    var postUrl = "rest/hello/treeData";

    var dsdata = $("#treeview").data("kendoTreeView").dataSource.data();
    var accID = "onthespot";
    var username = "alex";

    req.open("post", postUrl, true);
    req.setRequestHeader("Content-type","text/plain");

    req.send("data=" + JSON.stringify(dsdata) + "&username=" + username + "&accID=" + accID);

    req.onreadystatechange = function() {
        if (req.readyState != 4) {
            return;
        }
        if (req.status != 200) {
            alert("Error: " + req.status);
            return;
        }
        alert("Sent Data Status: " + req.responseText);
    }
}

服务器JAX-RS代码为:

And the Server JAX-RS code is:

@Path("/treeData")
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String storeTreeData(
        @QueryParam("data") String data,
        @QueryParam("username") String username,
        @QueryParam("accID") String accID) {

        System.out.println("Data= " + data + "\nAccID= " + accID + "\nUsername= " + username);

        return "Done";
}

问题在于所有变量都被打印为null. storeTreeData函数应该通过@QueryParam找到datausernameaccID变量并存储它们,不是吗?

The problem is that all the variables are printed as null.. the storeTreeData function should find the data , username , accID variables through @QueryParam and store them isn't that right?

有人知道这里出什么问题吗?

Anyone know what's the problem here?

PS:正确初始化xmlhttp请求并建立连接,但未在服务器上传递参数.

PS:The xmlhttp request is initiallized correctly and the connection is made but the parameters are not passed on the server.

推荐答案

您尝试执行的操作:

@QueryParam 用于从请求的查询中获取参数:

@QueryParam is used to get parameters from the query of the request:

http://example.com/some/path?id=foo&name=bar

在此示例中,idname可以作为@QueryParam访问.

In this example id and name can be accessed as @QueryParam.

但是您要在请求的 body 中发送参数.

But you are sending the parameters in the body of your request.

您应该做什么:

要从主体获取参数,应使用 @FormParam 以及 application/x-www-form-urlencoded :

To get the parameters from the body, you should use @FormParam together with application/x-www-form-urlencoded:

@Path("/treeData")
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
public Response storeTreeData(
        @FormParam("data") String data,
        @FormParam("username") String username,
        @FormParam("accID") String accID) {

    // Build a text/plain response from the @FormParams.
    StringBuilder sb = new StringBuilder();
    sb.append("data=").append(data)
      .append("; username=").append(username)
      .append("; accId=").append(accID);

    // Return 200 OK with text/plain response body.
    return Response.ok(sb.toString()).build();
}

您还应该使用

req.setRequestHeader("Content-type","application/x-www-form-urlencoded");

在您的JavaScript代码中.

in your JavaScript code.

这篇关于Jax-RS和Xmlhttp通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆