I have one json
file named fake.json inside assets in my angular application. Path of this file is like this.
MyApp => src => assets => json => fake.json
I want to make a POST
request to this file using HttpClient in my component which is in inside app folder.
MyApp => src => app => Statistics => statistics.component.ts
Component source code
export class StatisticsComponent {
persons: Person[];
options = {
sDom: 'rt<"bottom"p>',
pagingType: 'full_numbers',
pageLength: 10,
serverSide: true,
processing: true,
ajax: (dataTablesParameters: any, callback) => {
this.http
.post<DataTablesResponse>(
'./../../assets/json/fake.json',
dataTablesParameters, {}
).subscribe(resp => {
this.persons = resp.data;
callback({
recordsTotal: resp.recordsTotal,
recordsFiltered: resp.recordsFiltered,
data: []
});
});
},
columns: [
{ data: "id" },
{ data: "firstName" },
{ data: "lastName" }
]
};
constructor(private http: HttpClient) {
}
}
class Person {
id: number;
firstName: string;
lastName: string;
}
class DataTablesResponse {
data: any[];
draw: number;
recordsFiltered: number;
recordsTotal: number;
}
I occurred this following error
HttpErrorResponse Http failure response for http://localhost:4200/assets/json/fake.json: 404 Not Found
I got 2 doubts over this.
Is it valid to make a
POST
request to a local json file using Http or HttpClient. (Till now I have doneGET
request using Http not HttpClient and got the data successfully)Why it returns 404 Not Found when the file is present there inside the folder.
Need Help.
angular-cli
do you use? - Bunyamin Coskuner