angular 吧http请求封装return promise对象 怎么能拿到响应头
async request(method, url, data = {}) {
const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${localStorage.getItem('token')}`,
};
const trim = s => s === undefined || s === null ? '' : s;
const args: any = [[
// isDevMode() ? '/api' : '//yanduapi.t.oc1.xyz/v2',
window['baseUrl'],
url.replace(/:([^&;/?]+)/g, (...s) => trim(data[s[1]])),
].join('/')];
const params = Object.keys(data).reduce((obj, key) => {
obj[key] = trim(data[key]);
return obj;
}, {});
if (method === 'get' || method === 'delete') {
args.push({ headers, params });
} else {
args.push(params, { headers });
}
try {
const requestData_old = JSON.parse(localStorage.getItem('requestData'));
const requestData = {
url: args[0],
method: method,
timestamp: (new Date()).getTime()
}
return await this.http[method](...args).toPromise();
// }
} catch (e) {
console.log(e)
const { status, statusText } = e;
if (status != 401) {
this.notify.create('error', `${status}`, e.error.message);
}
if (status === 401) {
localStorage.clear();
this.util.navigate(['/user/login']);
}
throw e;
}
}
delete = (url) => (data?) => this.request('delete', url, data);
get = (url) => (data?) => this.request('get', url, data);
patch = (url) => (data?) => this.request('patch', url, data);
post = (url) => (data?) => this.request('post', url, data);
put = (url) => (data?) => this.request('put', url, data);
调用
async getData(index?) {
this.loading = true;
this.currentPage = index;
const data = await this.api.get('accounts')({
// search_all: this.search.value.searchAll,
page: this.currentPage,
'per-page': this.limits
}).then((res)=>{
console.log(res.header)
});
console.log(data)
// this.loading = false;
// this.data = data;
// this.totalCount = data._meta.totalCount;
}
如何能拿到响应头里的内容呢 求解
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
observe: 'response'