首页 > 解决方案 > Aurelia 没有收到带有 fetch 客户端的 json 响应

问题描述

我正在尝试使用 fetch 客户端来接收包含大型数组的 json 对象。我可以在控制台中看到包含该值的承诺,但是当尝试从响应中查看数据时,它是一个空对象 {}。我究竟做错了什么?

@inject(Router,HttpClientService)
export class GameMap {

  constructor(router,client) {
    this.client = client.client;
     firebase.auth().onAuthStateChanged((user) => {
      if (user) {
        this.user = user;
        user.getIdToken().then((idtoken) => this.client.configure(config => config.withDefaults(({headers: {'ID-TOKEN': idtoken}})))).then(() => this.requestMap());
      }
    });
  }

  requestMap() {
    this.client.fetch('game').then(response => json(response)).then((data) => console.log(data))
  }

}

HttpClientService

    export class HttpClientService {

  constructor(){
    this.client = new HttpClient();
    this.client.configure(config => {
      config
        .withBaseUrl('http://localhost:8808/')
        .withInterceptor({
          request(request) {
            console.log(request)
            console.log(`Requesting ${request.method} ${request.url}`);

            return request;
          },
          response(response) {
            console.log(`Received ${response.status} ${response.url}`);
            return response;
          }
        });
    });
  }



}

标签: javascriptaureliafetch-api

解决方案


推荐阅读