【问题标题】:How to get Rest Webservice Response header values in a jquery ajax call如何在 jquery ajax 调用中获取 Rest Webservice 响应标头值
【发布时间】:2018-02-20 10:20:49
【问题描述】:

我有一个使用 Jersey 的 rest web 服务实现,它在发布 XML 时返回一个响应。我在我的 jsp 中使用 jquery ajax 调用将 XML 内容发布到 Web 服务,如下所示。下面的代码工作正常,我得到了 XML 响应并调用了成功函数,我能够根据需要获取新发布的 XML 内容。

但是,我想在下面的成功函数中获取响应标头的值,例如内容类型、状态等。有没有办法我们可以做到这一点。当我使用邮递员进行验证时,我会看到内容类型、状态和其他详细信息。我想在ajax调用的成功函数中获取这些值。

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

}

下面是ajax调用:

            $.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){

                }
            });

任何帮助将不胜感激。

【问题讨论】:

  • 谢谢!!我可以通过执行“request.getResponseHeader("Content-type");”来引用这篇文章来获取 Content-type 的值,请告诉我如何获取状态(例如:201 Created)
  • 您将状态作为参数,为什么不打印它并查看它的值?您还可以打印“请求”对象中的任何内容,它应该包含状态信息。
  • status 没有提供像 201 created 这样的状态代码。它只是打印出我不想要的“成功”。我尝试了几个选项。请求对象也不包含这个

标签: java rest web-services jersey response


【解决方案1】:

我可以通过查看 Stack over flow 中的几篇帖子来解决这个问题。感谢 Elyor 发布上面的 cmets。在此处发布,以便可以帮助面临类似问题的人。

HTTP 响应代码: 通过执行以下操作,我能够获得响应状态、内容类型: 响应状态:在这种情况下,我正在寻找响应状态,HTTP 响应代码(201)。能够在“完成”回调函数中使用参数得到这个,如下所示

响应标头:“Content-type”之类的响应标头是从传递给“Success”函数的请求对象中获取的。

           $.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);
                }
            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 2016-07-25
    相关资源
    最近更新 更多