【问题标题】:How to take the header values in jquery [duplicate]如何在jquery中获取标题值[重复]
【发布时间】:2017-05-22 01:53:29
【问题描述】:
我在这里调用了一个 API,我得到了成功响应,但他们返回了总计数标题部分,我不知道如何获取这些值。
看这里:
$.ajax({
url: '//www.examples.com/api/get/',
type: 'GET',
contentType: 'application/json; charset=utf-8',
success: function(result) {
console.log(result);
},
error: function(errMsg) {
//console.log(errMsg);
}
});
【问题讨论】:
标签:
javascript
jquery
rest
api
【解决方案1】:
试试这个:request.getResponseHeader('x-Total-Count') 成功回调
$.ajax({
url: '//www.examples.com/api/get/',
type: 'GET',
contentType: 'application/json; charset=utf-8',
success: function(data, textStatus, request){
alert(request.getResponseHeader('x-Total-Count'));
},
error: function(errMsg) {
//console.log(errMsg);
}
});
【解决方案2】:
成功回调有3个参数,可以点击here
$.ajax({
url: '//www.examples.com/api/get/',
type: 'GET',
contentType: 'application/json; charset=utf-8',
success: function (result, textStatus, xhr) {
console.log(xhr.getResponseHeader('X-Total-Count'));
},
error: function(errMsg) {
//console.log(errMsg);
}
});