【发布时间】:2018-03-11 08:32:30
【问题描述】:
当尝试使用 fetch API(由 Aurelia-fetch-client 包装)从响应中检索标头时,标头对象为空;
这是我获取客户的方法
public GetCustomers(): Promise<CustomerList> {
let customerList = new CustomerList();
return this._http.fetch('/api/customers')
.then(response => {
let links = response.headers.get('Link');
customerList.pagination.pageCount = parseInt(response.headers.get('X-Pagination.pageCount'));
customerList.pagination.pageNumber = parseInt(response.headers.get('X-Pagination.pageNumber'));
customerList.pagination.pageSize = parseInt(response.headers.get('X-Pagination.pageSize'));
customerList.pagination.totalItems = parseInt(response.headers.get('X-Pagination.totalItems'));
console.log(response.headers);
return response.json() as any
}).then(data =>{
data.map(
item => {
let customer: Customer = new Customer(this);
Object.assign(customer, item);
customerList.customers.push(customer);
});
return customerList;
});
}
Http Fetch 配置
constructor(private eventAggregator: EventAggregator, url: string) {
this._http = new HttpClient();
this._http.configure(config => {
config
.withBaseUrl(url)
.withInterceptor({
request(request) {
console.log(`Requesting ${request.method} ${request.url}`);
return request; // you can return a modified Request, or you can short-circuit the request by returning a Response
},
response(response) {
console.log(`Received ${response.status} ${response.url}`);
return response; // you can return a modified Response
}
})
.withDefaults({
'mode' : 'cors',
headers: {
'Authorization': `Bearer ${localStorage.getItem('access_token')}`
}
})
.useStandardConfiguration();
});
}
这是我们可以在 Chrome 中看到的内容。我正在公开我想要的标题。
【问题讨论】:
-
你能从你的回复中看到任何标题吗?
-
无。在 chrome 调试会话中,标题是一个空对象。
-
我刚刚测试过,我得到了同样的结果。如果您改用标准
aurelia-http-client,那么您将能够看到我怀疑的它们。但是,这可能缺少您需要的其他功能。 -
干杯,我会给它一个狂欢。听起来像是一个潜在的错误?
-
@MrBliz - 找到答案?
标签: javascript http typescript aurelia fetch-api