【问题标题】:Pass parameter data from JQuery/Ajax call to Jersey web service将 JQuery/Ajax 调用中的参数数据传递给 Jersey Web 服务
【发布时间】:2015-08-24 04:10:48
【问题描述】:
            $.ajax({url:"http://localhost:8080/TestApp/resources/Test",
                type:"GET",
                data:userName :"userName",
                cache: false,
                success:function(result){   
                    alert(result);
                },
            });

上面是我用来调用服务的 JQuery 代码。如何将userName 参数传递给

    @GET
    @Produces("text/html")
    public String getHtml() {

        return "Hello ";
    }

类似于 servlet 中的 request.getParameter("")。在我的理解中,@QueryParam@PathParam@FormParam 用于不同的目的,或者它们可以用于获取参数。 (我已经尝试了所有三个并且失败了)。如果我做错了什么,请纠正我。

RuntimeException 无法映射到响应,重新抛出到 HTTP 容器 com.sun.jersey.api.container.ContainerException: 异常获取参数在 com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:54) 在 com.sun.jersey.server.impl.model.method.dispatch.Abs​​tractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:137) 在 com.sun.jersey.server.impl.model.method.dispatch.Abs​​tractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:165) 在 com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:70) 在 com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:279) 在 com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:86) 在 com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:136)

如上所做的更改并获得异常。

【问题讨论】:

    标签: java jquery ajax jersey


    【解决方案1】:

    data 属性必须是纯 js 对象、String 或数组(参见http://api.jquery.com/jquery.ajax/

    所以,在你的代码中,数据元素应该是

    data:{userName:'UserName'}
    

    【讨论】:

    • @QueryParam 给出了正确的值,并且在您更改之后它工作正常。非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-18
    • 2019-05-19
    • 2010-12-27
    • 1970-01-01
    • 2011-05-13
    相关资源
    最近更新 更多