0

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.

enter image description here

@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.

Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105
Awanish
  • 327
  • 1
  • 3
  • 14
  • Possible duplicate of https://stackoverflow.com/questions/1557602/jquery-and-ajax-response-header – elyor Feb 20 '18 at 10:27
  • Thank you!! I was able to get the value of Content-type by referring to this post by doing "request.getResponseHeader("Content-type");", can you please let me know how can I get the status (eg: 201 Created) – Awanish Feb 20 '18 at 10:42
  • 1
    You're getting the status as parameter, why not print it and see its value? you can also print whatever is in "request" object, it should contain the status info. – elyor Feb 20 '18 at 12:07
  • status does not give a status code like 201 created.. it just prints "success" that is not what I want.. and I have tried several options.. the request object does not contain this either – Awanish Feb 20 '18 at 13:18
  • 1
    you can try this one: https://stackoverflow.com/questions/10042040/jquery-ajax-statuscode-else – elyor Feb 20 '18 at 15:17
  • Thanks Elyor!! I got the required details now.. – Awanish Feb 21 '18 at 14:10

1 Answers1

0

I was able to solve this by looking at several posts in Stack over flow.. Thanks to Elyor for posting the comments above. Posting here so that it could help someone facing similar issue.

HTTP Response Code: I was able to get response status, content-type by doing below: Response status: I was looking to get the Response status, HTTP response code(201) in this case. Was able to get this using parameter in "Complete" callback function as below

Response Headers: The response headers like "Content-type" is fetched from request object that is passed to "Success" function.

           $.ajax({
                type: "POST",
                url: "http://localhost:9090/messenger/webapi/messages",
                data: message,
                contentType: "application/xml; charset=utf-8",
                dataType: "xml",
                success: function(msg, status, request) {

                    alert("request: "+request.getResponseHeader("Content-type"));

                    $(msg).find("autor").each(function(){
                        alert($(this)[0].childNodes[0].nodeValue);
                    });

                },
                error: function(e){

                },
                complete: function(e, xhr, settings){
                    alert("Response status: "+e.status);
                }
            });
Awanish
  • 327
  • 1
  • 3
  • 14