【问题标题】:Headers null when making a CORS request with Aurelia-Fetch-Client使用 Aurelia-Fetch-Client 发出 CORS 请求时,标头为空
【发布时间】: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


【解决方案1】:

据我了解,在服务器上使用 CORS 策略时,您需要专门选择加入响应中的标头。 ASP.NET Core 对此有一个“WithExposedHeaders”方法 https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.cors.infrastructure.corspolicybuilder.withexposedheaders?view=aspnetcore-2.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 2017-11-06
    • 2019-12-07
    • 2017-12-17
    • 2016-03-12
    相关资源
    最近更新 更多