[Angular] Fetch non-JSON data by specifying HttpClient responseType in Angular

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Angular] Fetch non-JSON data by specifying HttpClient responseType in Angular相关的知识,希望对你有一定的参考价值。

By default the new Angular Http client (introduced in v4.3.1) uses JSON as the data format for communicating with the backend API. However, there might be situations where you may want to use some other format, like text/plain for fetching a CSV file. Using the responseType property this can be achieved quite easily.

 

import { Injectable } from ‘@angular/core‘;
import { Observable } from ‘rxjs/Observable‘;
import { HttpClient, HttpErrorResponse, HttpHeaders } from ‘@angular/common/http‘;

@Injectable()
export class PeopleService {

  constructor(private http: HttpClient) {}

  fetchPeople(): Observable<Object> {
    return this.http
      .get(‘data/people.txt‘, { responseType: ‘text‘});
  }

}

 

以上是关于[Angular] Fetch non-JSON data by specifying HttpClient responseType in Angular的主要内容,如果未能解决你的问题,请参考以下文章

typescript Angular订阅查询param id和fetch API

如何使用 Angular 发送带有对象参数的 fetch api 请求?

将 Javascript Fetch 请求转换为 Angular Http 客户端不起作用

AJAX的第二春!fetch!!!

无法使用 Fetch API 获取 JSON,但可以使用 JQuery

Angular ngrx - 显示加载中的 gif