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

javascript - No overload matches this call. The last overload gave the following error. Type '"text"&a

programmeradmin10浏览0评论

I am trying to GET a HTML page from my API using Angular with JWT tokens in the header. But if I don't specify the text type the compiler spits out a cannot parse error (it tries to parse the HTML as JSON then), so I need to specify my response type.

Here is my component.ts:

  httpOptions = {
    headers: new HttpHeaders({
      'Content-Type':  'application/json',
      'Authorization': 'Bearer TOKENHERE'
    })
  };
  constructor(private http: HttpClient) { }

  ngOnInit() {


  }
  fetchDashboard() {
    this.http.get('http://localhost:3000/avior/dashboard', {responseType: 'text', this.httpOptions,}).subscribe(j => console.log(j));
}
}

I tried removing or changing the content-type to text/html without any avail.


My API Response:

<html><head><title>Dashboard</title></head><body><h1>Dashboard works!</h1></body></html>

I am trying to GET a HTML page from my API using Angular with JWT tokens in the header. But if I don't specify the text type the compiler spits out a cannot parse error (it tries to parse the HTML as JSON then), so I need to specify my response type.

Here is my component.ts:

  httpOptions = {
    headers: new HttpHeaders({
      'Content-Type':  'application/json',
      'Authorization': 'Bearer TOKENHERE'
    })
  };
  constructor(private http: HttpClient) { }

  ngOnInit() {


  }
  fetchDashboard() {
    this.http.get('http://localhost:3000/avior/dashboard', {responseType: 'text', this.httpOptions,}).subscribe(j => console.log(j));
}
}

I tried removing or changing the content-type to text/html without any avail.


My API Response:

<html><head><title>Dashboard</title></head><body><h1>Dashboard works!</h1></body></html>

Share Improve this question edited Nov 19, 2019 at 10:02 Munchkin asked Nov 19, 2019 at 9:47 MunchkinMunchkin 1,0855 gold badges30 silver badges64 bronze badges 18
  • 1 Is the Content-type header correct? What headers do you pass in while hitting this API through a client like POSTMAN? – Nicholas K Commented Nov 19, 2019 at 9:49
  • @NicholasK in Postman I only specify the Authorization header with the token in format Bearer <tokenhere> – Munchkin Commented Nov 19, 2019 at 9:50
  • httpCilentModule will parse your request by default so you will have to specify the content type – Joel Joseph Commented Nov 19, 2019 at 9:54
  • 1 Also, why do you specify a Content-Type in a GET request? AFAIK that is only for PUT, PATCH, POST (requests that actually send data in their body). – Christoph Commented Nov 19, 2019 at 9:56
  • 1 There is no need to pass in Content-Type as a header, as per your previous comment. – Nicholas K Commented Nov 19, 2019 at 10:04
 |  Show 13 more comments

2 Answers 2

Reset to default 8

I did it this way:

    getDivisionName(x: string): Promise<any> {
    return this.httpClient
      .get<any>(this.serviceUrl + `/something/${x}/`, {
        responseType: 'text' as 'json',
      })
      .toPromise();
  }

You could try the following on in your component to see if your call works to another website. The following should return the html of the requested page as a string. If this does not work with your API then perhaps the problem is not your code in the component, but the response your API is returning

const requestOptions: Object = {
  headers: new HttpHeaders().append('Authorization', 'Bearer <yourtokenhere>'),
  responseType: 'text'
}

this.http.get<string>('https://cors-anywhere.herokuapp.com/https://stackoverflow.com/questions/tagged/angular', 
    requestOptions)
        .subscribe(response => {
               console.log(response);
        }
);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论