I have a rest web service implementation using Jersey which returns a Response on posting an XML. I am using jquery ajax call in my jsp to post the XML content to the web service as below. The below code works fine and I get the XML response and the success function gets called, I am able to get the newly posted XML content as required.
However, I would like to get the values of response headers like content-type, status etc in the success function below. Is there a way we can achieve this. I see the content-type, status and other details when I verify this using postman. I would like to get these values in the success function of ajax call.
@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
public Response postMessage(Message msg, @Context UriInfo uriInfo){
msgService.addMessage(msg);
URI uri = uriInfo.getAbsolutePathBuilder().path(String.valueOf(msg.getId())).build();
Response rs = Response.created(uri)
.entity(msg)
.status(Status.CREATED)
.build();
System.out.println(rs);
return rs;
}
Below is the ajax call made:
$.ajax({
type: "POST",
url: "http://localhost:9090/messenger/webapi/messages",
data: message,
contentType: "application/xml; charset=utf-8",
dataType: "xml",
success: function(msg, status) {
$(msg).find("autor").each(function(){
alert($(this)[0].childNodes[0].nodeValue);
});
//Would like to alert the reponse headers like content-type received and status (201 in this case) here..
},
error: function(e){
}
});
Any help would be appreciated.