使用JAX-RS和jquery .ajax()检索参数

我正在使用jax-rs编写基本的Web服务。 我正在使用以下JQuery测试应用程序。 我可以使用GET参数访问参数,但是当我使用POST时,它会失败。 我使用以下JQuery来测试只将“POST”切换为“GET”。

$.ajax({ type: "POST", url: "http://localhost:8080/MyWebService/", data: 'text=this is text', dataType: 'jsonp', success: function(data){ console.log(data); }, error: function(){alert('failure');} }); 

java端看起来如下所示。 请记住,我对获取部分没有任何问题。

 @GET public Response getWork(@QueryParam("callback") String callbackName, @QueryParam("text") String searchText, @Context HttpServletResponse response, @Context HttpServletRequest request) throws JsonGenerationException, JsonMappingException, IOException { System.out.println(searchText); return work(callbackName, searchText, response, request); } @POST public Response postWork(@FormParam("callback") String callbackName, @FormParam("text") String searchText, @Context HttpServletResponse response, @Context HttpServletRequest request) throws JsonGenerationException, JsonMappingException, IOException { System.out.println(searchText); return work(callbackName, searchText, response, request); } 

使用GET时,它会打印文本,但在使用POST时,它会打印null。 使用POST方法访问参数需要什么?

如果您发送表单数据,则应指定将使用的内容服务器类型。 因此,您应该使用@Consumes(MediaType.APPLICATION_FORM_URLENCODED)注释POST方法。 换句话说,POST方法应该是这样的:

 @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) // rest of the code