I have a local JSON file that I'm trying to get access to through an HTTP GET request. I put it in my-project/src/app/assets/accounts.json and checked the JSON is good.
I perform the request from a service that is called by ngOnInit() - I have also tried without the 2nd parameter in the get
public getFieldsFromFile() {
const url = "assets/accounts.json";
this.http.get(url, {responseType: "text"})
.subscribe((data) => console.log(data));
}
Unfortunately all I get in my console.log is this Error message
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://users.local/assets/accounts.json", ok: false, …}
error: {error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttp…, text: "<!DOCTYPE html>↵<html>↵ <head>↵ <title>U…<script src="dist/js/users.js"></script>↵</html>↵"}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure during parsing for http://users.local/assets/accounts.json"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "http://users.local/assets/accounts.json"
If I expand, what I see is the HTML code of the index.html:
<!DOCTYPE html>
<html>
<head>
<title>Users</title>
<base href="/" />
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="dist/css/vendor.css">
<link rel="stylesheet" href="dist/css/users.css">
</head>
<body>
<mv-users id="app-users">
Loading...
</mv-users>
</body>
<script src="dist/js/vendor.js"></script>
<script src="dist/js/users.js"></script>
</html>
I understand this might be a router issue? Which is strange because I don't have any routes redirecting to index.html:
{
path: "users",
component: UsersComponent,
},
{ path: "**", redirectTo: "users" },