如何在Angular 2中附加标题以获得http?

时间:2018-11-22 10:33:48

标签: angular http service response angular2-services

我从json文件中接收到奇怪的字符,所以我猜编码方面存在问题,因此我需要向该服务中的http获取请求添加标头 我认为是这样

headers.append('Content-type','application / json; charset = UTF-8');

这是我的源服务代码:

import { Component, Input } from '@angular/core';
import { Injectable }     from '@angular/core';
import { Http, Response, Headers, RequestOptions } from 
'@angular/http';
import { HttpClient } from '@angular/common/http';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

@Injectable()
export class listsService{

 constructor(private http: HttpClient) {   }
   //get data from json file
  getList(){
   return this.http.get('../data.json')
  .catch(err => {return "no data";});
 }

}

0 个答案:

没有答案