-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.

1
  • 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 1, 2021 at 13:32

1 Answer 1

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' });
 }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.