首页 > 解决方案 > 服务器上的 Angular 可观察问题

问题描述

我在 Angular 6 中创建了一个服务,该服务通过订阅在 ngOnInit 上运行良好,但是一旦我尝试通过函数/单击事件获得响应,我就会收到以下错误。

错误 DOMException:无法在“XMLHttpRequest”上设置“responseType”属性:无法更改从文档发出的同步请求的响应类型。

我怀疑我在做一些愚蠢的事情 - 数据在 fetch 中返回正常。然后调用所以我排除了服务器配置,到目前为止仍在通过 Angular 理论工作,所以在这里发布以防万一它对某人来说是显而易见的更多经验。

localhost 它工作正常,但是当我将代码构建/移动到服务器时,出现错误 - 有什么想法吗?

亲切的问候雷切尔

服务

export class GetVehicleResultsService {
  url = 'SOME_URL';
  constructor(private http: HttpClient) {}

  getVehicles() {
    return this
      .http
      .get<Vehicle>(`${this.url}`, {
        responseType: 'json',
        params: {
          keyword: '*'
        }
    });
  }
}

零件

 ngOnInit() {
    this
      .getVehicleResultsService
      .getVehicles()
      .subscribe(
        vehicles => {
          this.vehicles = vehicles.response.docs;
          this.facets = vehicles.facet_counts.vcj_facet_fields;
      });
  }
// this call works and consoles the data 
  test() {

    fetch('SOME_URL')
  .then(response => response.json())
  .then(json => console.log(json));
  }
// this doesn't work

test() {
this
          .getVehicleResultsService
          .getVehicles()
          .subscribe(
            vehicles => {
              this.vehicles = vehicles.response.docs;
              this.facets = vehicles.facet_counts.vcj_facet_fields;
          });

      }

标签: angularhttpangular2-observables

解决方案


推荐阅读