0

Please find the below url.

https://stackblitz.com/edit/angular-w4lj6q?file=app%2Fapp.component.ts

I am using http module and trying to get the data from json file but i am getting below error. can you please help somebody to fix the issue.

Error : errors.js:55 ERROR SyntaxError: Unexpected token < in JSON at position 0

0

2 Answers 2

1

There are couple of things going wrong in the stackblitz.

  1. The folder which contains the JSON is apiDta instead of apiData
  2. Stackblitz is not returning the JSON even with the correct URL, rather responds with a HTML.

You can use reqres to get a sample JSON. Update the URL in NameListService

private _url: string = "https://reqres.in/api/users";

and update AppComponent's ngOnInit()

this._nameService
    .getNames()
    .subscribe(res => { 
                          this.nameLists = res;
                          console.log(JSON.stringify(this.nameLists));
                      });
Sign up to request clarification or add additional context in comments.

1 Comment

I can confirm, this works. Also nice find with reqres :)
0

because you should add the content-type equal to json in the http header to make sure that the returned data format is json not HTML as your code do

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.