-1

If response coming from my API is in JSON format then it works fine but if in non-json format then the function does not work. Here is what I am doing -

In my page.ts

import { ApiconnectService } from '../../apiconnect.service';

export class BinaryGraphPage implements OnInit {
  value : any;
  constructor(
    private cs: ApiconnectService
  ) { }

  userGraph(){
     this.cs.unilevelGraph().subscribe(response =>{
         this.value = response;
     })
  }

}

In apiconnect.service.ts

  unilevelGraph(){
    return this.http.get(this.url+'?sponser='+uid);
  }

The response coming from API is not in JSON format (I tried JSON format and it works fine but for some reason my response need to be in text/string).

In API, response is a long text and contains html tags such as br tag, span and li tag e.g.: Howdy user, this is your graph list 1.item, 2. item, 3.item, etc.

Since response is not in JSON format, so this errors appear in my console. Error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse () at XMLHttpRequest.onLoad (http://......

Can you please suggest me how to rewrite the function userGraph() so that it can work with string or text.

R. Richards
  • 24,603
  • 10
  • 64
  • 64
Bips
  • 113
  • 2
  • 16
  • 1
    Does this answer your question? [Angular 6: How to set response type as text while making http call](https://stackoverflow.com/questions/50798592/angular-6-how-to-set-response-type-as-text-while-making-http-call) – Drenai May 01 '21 at 13:16
  • 1
    @Drenai Yes, this may answer my question. I have marked the below answer as "accepted answer" because it seems straight forward for user like me with beginner skill – Bips May 01 '21 at 13:32

1 Answers1

1

Since you are not getting a JSON response, specify the response type in the options. So, the service method becomes:

 unilevelGraph(){
    return this.http.get((this.url+'?sponser='+uid), { responseType: 'text' });
 }
sofa_maniac
  • 1,659
  • 2
  • 12
  • 21