【发布时间】:2019-07-19 15:11:58
【问题描述】:
我的 Angular 应用程序的资产中有一个名为 fake.json 的 json 文件。这个文件的路径是这样的。
MyApp => src => assets => json => fake.json
我想在 app 文件夹内的组件中使用 HttpClient 向该文件发出 POST 请求。
MyApp => src => app => Statistics => statistics.component.ts
组件源代码
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;
}
我发生了以下错误
HttpErrorResponse http://localhost:4200/assets/json/fake.json 的 Http 失败响应:404 Not Found
我对此有两个疑问。
使用 Http 或 HttpClient 向本地 json 文件发出
POST请求是否有效。 (到目前为止,我已经使用 Http 而不是 HttpClient 完成了GET请求并成功获取数据)为什么当文件存在于文件夹中时它返回 404 Not Found。
需要帮助。
【问题讨论】:
-
您使用哪个版本的
angular-cli? -
@BunyaminCoskuner Angular CLI 版本 1.6.1
标签: angular angular-httpclient