Ajax call to REST service
drwho Nov 20, 2012 12:55 PMHI,
I created a portlet to call a REST service . Below is the code for the javascript:
<body> <form id="myform"> <input type="text" id="keyword"/> <div id="target">Save Data</div> </form> <script type="text/javascript"> // dummy submit function jQuery(document).ready(function () { $("#target").click(function() { var rootURL = "rest/request"; alert ('value is '+$('#keyword').val() ); $.ajax({ type: 'PUT', contentType: 'application/json', url: rootURL + '/address' , // dataType: "json", data: formToJSON(), success: function(data, textStatus, jqXHR){ alert('Address updated successfully. Status: '+textStatus); }, error: function(jqXHR, textStatus, errorThrown){ alert('updateAddress error: ' + textStatus); } }); function formToJSON() { return JSON.stringify({ "address": $('#keyword').val() }); } }); }); </script> </body> Here it the service I am trying to call:
@Path("/address/{address}/")
@PUT
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response putAddress(@PathParam("address") String address) throws Exception {
requestObject.setAddress(address);
return Response.ok("Address is " + requestObject.getAddress() + " !").build();
}
After I made the Ajax call which returns a success I go the following exception :
2012-11-20 10:26:50,172 ERROR [portal:UIPortletLifecycle] (http-127.0.0.1-8080-4) Portlet render threw an exception
org.exoplatform.services.portletcontainer.PortletContainerException: java.lang.NullPointerException
However the Ajax call was not successful when I queried my Java object.
Please see attached server log.
What could be causing the problem? How do I make it work?
Gordon
-
server.log.zip 12.6 KB