Search code examples
javajavascriptrestprototypejs

How to pass parameter from prototypejs client to rest web service


I have a rest web service like

@Path("/postItem")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Item postItem(@QueryParam("name") String name, @QueryParam("price") String price)
{
  System.out.println(name);
  System.out.println(price);
  return new Item(name , price);
}

And I use prototypejs javascript lib to invoke above rest web service from the client side with below code snippet.

<script>
  new Ajax.Request('/some_url', {
    method:'post',
    parameters: {name: 'apple', price: 12}
    onSuccess: function(transport) {
    var response = transport.responseText || "no response text";
    alert("Success! \n\n" + response);
    },
    onFailure: function() { alert('Something went wrong...'); }
 });
</script>

Problem : I am not able to correctly pass the parameter to name and price of the service method.

I am passing two parameters in client but in service side only the parameter 'name' is getting mapped(that too with wrong value). when i print the name and price i get the following

 System.out.println(name);  ==> name='apple'&price=12
 System.out.println(price); == null

How can i pass parameter to service from prototypejs client so that 'name' gets the value apple and 'price' gets the value 12.


Solution

  • Ok finally it worked by modifying the url.

    <script>
       new Ajax.Request('/some_url?name=apple&price=12', {
       method:'post',
       onSuccess: function(transport) {
       var response = transport.responseText || "no response text";
       alert("Success! \n\n" + response);
     },
       onFailure: function() { alert('Something went wrong...'); }
    });
    </script>