jQuery-获取AJAX响应标头 [英] jQuery - get AJAX response headers
问题描述
当我们使用jQuery触发ajax请求时,如何访问响应标头?根据某些网站提供的建议,我尝试使用以下代码.但是xhr
对象作为null来了.我在此上下文中看到一个xhr
对象.但是它没有访问响应头的方法.
How do we get access to the response headers when we fire an ajax request using jQuery? I tried with the below code as per the suggestions given in some sites. But xhr
object is coming as null. I see an xhr
object in this context. But it doesn't have methods to access response headers.
function SampleMethod(){
var savedThis=this;
this.invokeProcedure=function(procedurePath){
$.ajax({
type: "GET",
url: procedurePath,
dataType: "json",
success: function(data,status,xhr){savedThis.resultSetHandler(data,status,xhr);}
});
}
this.resultSetHandler=function(data,status,xhrObj){
//Handle the result
}
this.errorHandler=function(args){
//Handle the result
}
}
var sampleObj=new SampleMethod();
sampleObj.invokeProcedure('url');
推荐答案
为了与XMLHttpRequest向后兼容,将使用jqXHR对象 公开以下属性和方法: getAllResponseHeaders() 和 getResponseHeader(). 从$ .ajax()文档中: http://api.jquery.com/jQuery.ajax/ >
For backward compatibility with XMLHttpRequest, a jqXHR object will expose the following properties and methods: getAllResponseHeaders() and getResponseHeader(). From the $.ajax() doc : http://api.jquery.com/jQuery.ajax/
对于jQuery> 1.3
For jQuery > 1.3
success: function(res, status, xhr) {
alert(xhr.getResponseHeader("myHeader"));
}
这篇关于jQuery-获取AJAX响应标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!