【问题标题】:Sending array along with javascript call to the Driver class in java -rest将数组与 javascript 调用一起发送到 java -rest 中的 Driver 类
【发布时间】:2017-12-10 15:52:33
【问题描述】:

我正在尝试将四个项目的数组添加到驱动程序类的调用中。我的 JavaScript 代码是这样工作的:

request.open("GET","rest/ds/acceptInput?UserInput="+document.getElementById('text').value, async=true);
request.send()

我使用它来将用户输入发送给驱动程序,并且这工作带有带注释的驱动程序(见下文)。

@Test
@Path("acceptInput")
@GET
@Consumes("text/plain")
@Produces(MediaType.APPLICATION_JSON)
public String acceptInput(@QueryParam("UserInput") String input{

现在我希望能够在调用驱动程序的同时发送一个数组(通过 JavaScript 中的用户复选框获得)。我尝试了几种方法,但对我来说有意义的是:

JavaScript 代码如下:

request.open("GET","rest/ds/acceptInput?UserInput="+document.getElementById('text').value + checked[0],checked[1],checked[2],checked[3], async=true);
request.send()

而司机接受它是这样的:

    @Test
    @Path("acceptInput")
    @GET
    @Consumes("text/plain")
    @Produces(MediaType.APPLICATION_JSON)
    public String acceptInput(@QueryParam("UserInput") String input, @QueryParam("po") boolean Po, @QueryParam("cl") boolean Cl, @QueryParam("ac") boolean Ac, @QueryParam("qu") boolean Qu) {

但现在我最终没有返回任何结果。任何想法为什么会发生这种情况?

【问题讨论】:

    标签: javascript java rest


    【解决方案1】:

    将您的 JavaScript 代码更改为:

    request.open("GET","rest/ds/acceptInput?UserInput="+document.getElementById('text').value + "&po="+ checked[0]+"&cl="+checked[1]+"&ac="+checked[2]+"&qu="+checked[3], true);
    

    您没有为 GET 请求中的复选框值提供任何参数名称。

    另外,不是async = true参数不应该只是true吗?

    【讨论】:

    • 叹息。很简单。谢谢。这工作得很好。 (我不擅长 javascript...我只用它来测试我的网络服务)
    • 不确定 async =true vs just true 那是另一个好问题
    • @Jason 是的 javascript 也不是我最喜欢的(主要是因为它不是强类型),但它是 Web 开发的必需品
    • 令人讨厌的部分是作为一个具有 webservice 3rd 方的 java 开发人员......
    猜你喜欢
    • 1970-01-01
    • 2013-04-17
    • 2016-05-04
    • 1970-01-01
    • 2017-09-26
    • 2020-05-26
    • 2015-01-29
    • 2012-10-14
    • 2016-11-05
    相关资源
    最近更新 更多