2015-06-09 95 views
0
  $.ajax({url:"http://localhost:8080/TestApp/resources/Test", 
       type:"GET", 
       data:userName :"userName", 
       cache: false, 
       success:function(result){ 
        alert(result); 
       }, 
      }); 

以上是我用来调用服务的JQuery代码。我怎样才能获得userName参数到将来自JQuery/Ajax调用的参数数据传递给Jersey web服务

@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) 在 融为一体。 sun.jersey.server.impl.model.method.dispatch.Abs​​tractResourceMethodDispatchProvider $ TypeOutInvoker._dispatch(AbstractResourceMethod DispatchProvider.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)

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

回答

1

数据属性必须是一个普通的js对象,字符串或阵列(参见http://api.jquery.com/jquery.ajax/

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

data:{userName:'UserName'} 
+0

@QueryParam得到正确价值,并在您的更改后它工作正常。非常感谢 :) – Yasothar