当我们使用 jQuery 触发 ajax 请求时,如何访问响应 header ?我根据某些网站给出的建议尝试使用以下代码。但是 xhr 对象为空。我在此上下文中看到一个 xhr
对象。但它没有访问响应 header 的方法。
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');
最佳答案
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
success: function(res, status, xhr) {
alert(xhr.getResponseHeader("myHeader"));
}
关于jQuery - 获取 AJAX 响应 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11440918/