[英]How to get Rest Webservice Response header values in a jquery ajax call
我有一個使用Jersey的其余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){
}
});
任何幫助,將不勝感激。
通過查看Stack over flow中的幾篇文章,我能夠解決此問題。.感謝Elyor在上面發表了評論。 張貼在這里,這樣可以幫助面臨類似問題的人。
HTTP響應代碼:通過執行以下操作,我可以獲取響應狀態,內容類型:響應狀態:在這種情況下,我希望獲取響應狀態,HTTP響應代碼(201)。 可以使用“完成”回調函數中的參數來獲取此信息,如下所示
響應標頭:從請求對象中獲取諸如“ Content-type”之類的響應標頭,並將其傳遞給“成功”功能。
$.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);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.