最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can't get http response as a string in Angular 5 - Stack Overflow

programmeradmin4浏览0评论

Given the source Angular Service code

@Injectable()
export class HomeContentService {

  constructor(private http: HttpClient) { }

  getContent(): Observable<string> {

    let fileURI = '/assets/content/home.md';
    let opt = { responseType: 'text' };
    return this.http.get<string>(
      fileURI,
      opt
    );

  }

Calling getContent() should return an Observable<string>. However, when I run ng serve, I get Type 'string' is not assignable to type 'json'.

After a few tries webpack seems to pile fine without me doing any changes, but the problem happens again anytime I close the process and try to ng serve again.

I guess there's something wrong with my setting responseType: 'text', but I don't know what it is. Googling wasn't helpful either.

Any ideas?

Given the source Angular Service code

@Injectable()
export class HomeContentService {

  constructor(private http: HttpClient) { }

  getContent(): Observable<string> {

    let fileURI = '/assets/content/home.md';
    let opt = { responseType: 'text' };
    return this.http.get<string>(
      fileURI,
      opt
    );

  }

Calling getContent() should return an Observable<string>. However, when I run ng serve, I get Type 'string' is not assignable to type 'json'.

After a few tries webpack seems to pile fine without me doing any changes, but the problem happens again anytime I close the process and try to ng serve again.

I guess there's something wrong with my setting responseType: 'text', but I don't know what it is. Googling wasn't helpful either.

Any ideas?

Share Improve this question asked Dec 1, 2017 at 18:25 fizzydrinksfizzydrinks 6341 gold badge7 silver badges19 bronze badges 3
  • use JSON.stringify(response) – Aravind Commented Dec 1, 2017 at 18:27
  • The problem happens before I can call anything on the result. If I return JSON.stringify(this.http.get...), will the Angular ponent be able to subscribe to the Observable? – fizzydrinks Commented Dec 1, 2017 at 18:31
  • Also it's not a JSON string so it seems wrong to stringify it. The requested file is just Markdown text. – fizzydrinks Commented Dec 1, 2017 at 18:32
Add a ment  | 

3 Answers 3

Reset to default 10

You have to inline the options as this is the only way that the TypeScript transpiler can infer that you are wanting to return type Observable<string>.

getContent(): Observable<string> {
  let fileURI = '/assets/content/home.md';
  return this.http.get(fileURI, { responseType: 'text' });
}

See also Http ResponseType cannot be set #18586, refer to the ments made by alxhub.

Try let opt = { responseType: 'text' as 'text' };

Explanation: https://github./angular/angular/issues/18586

i think u are not specifying map. Try this:

getContent ( ) : Observable< string > {
return this.http.get( YOUR REF )
       .map((res:Response) => res.text())
       .catch((error:any) => Observable.throw(error.json().error || 'Server error'));
} 

in res.text u specify the type, if the response was json ll be "res.Json()"

Sry for the Bad english

发布评论

评论列表(0)

  1. 暂无评论