[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‘});
  }

}

原文地址:https://www.cnblogs.com/Answer1215/p/8448902.html

时间: 2024-10-28 17:12:36

[Angular] Fetch non-JSON data by specifying HttpClient responseType in Angular的相关文章

Google Volley: How to send a POST request with Json data?

sonObjectRequest actuallyaccepts JSONObject as body. From http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/ final String url = "some/url"; final JSONObject jsonBody = /* ... */; new JsonObjectRequest(url, jsonBody,

使用 angular directive 和 json 数据的 D3 带标签 donut chart示例

利用angular resource加载priorityData.json中的json数据,结合D3画出甜甜圈图.运行index.html结果如图所示: priorityData.json中json数据如下: { "priority":{ "Blocker":12, "Critical":18, "Major":5, "Minor":30, "Trivial":24 } } index.

fetch获取json的正确姿势

fetch要求参数传递,遇到请求无法正常获取数据,网上其他很多版本类似这样: fetch(url ,{ method: 'POST', headers:{ 'Accept': 'application/json, text/plain, */*', 'Content-Type': 'application/json' }, body: JSON.stringify({a:1,b:2}) }).then(function(response){ return response.json(); }).

PHP convet class to json data

/********************************************************************* * PHP convet class to json data * 说明: * 突然想使用class自动转换为json数据,这样的代码可扩展性会好一点, * 只需要修改class的属性就能够达到最终json数据输出,不过有遇到class中 * 初始化class变量需要在构造函数中初始化的的问题. * * 2017-8-11 深圳 龙华樟坑村 曾剑锋 ***

键值对参数转换为Jquery json data参数

键值对:var postData = new List<KeyValuePair<string, string>>();postData.Add(new KeyValuePair<string, string>("a", "1"));postData.Add(new KeyValuePair<string, string>("b", "2"));postData.Add(new

jQuery解析JSON出现SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data 我在使用$.parseJSON解析后台返回的JSON的数据时,出现了这样的错误,我还以为返回的JSON格式出现了错误,因为JSON要求格式非常严格.最后发现JSON格式没有太明显的格式错误,我使用fastJSON来生成的JSON格式数据,原来是因为数据已经是一个JavaScript对象了,所以在进行解析就会出错了 我直接将这段数据al

json data 解析demo

json data: demo: JsonObject jsonObject= JsonHandle.getAsJsonObject(city_dataInfo).get("data").getAsJsonObject().get("cityList").getAsJsonObject().get("items").getAsJsonObject(); Set<Entry<String, JsonElement>> ent

SyntaxError: JSON.parse: bad control character in string literal at line 1 column 16 of the JSON data

JSON.parse转化Json字符串时出现:SyntaxError: JSON.parse: bad control character in string literal at line 1 column 16 of the JSON data 测试代码: JSON.parse("{\"Result\":\"bhbh\thuhuha\"}") 罪魁祸首:\t导致

directly receive json data from javascript in mvc

if you send json data to mvc,how can you receive them and parse them more simply? you can do it like this: latestData = []; $('.save').click(function () { $('.content tr').each(function () { var item = { id:null,date: '', weekday: '', holiday: ''}; l