Angular 2,将方法发布到本地json文件

Angular 2,将方法发布到本地json文件,angular,typescript,post,http-post,angular2-http,Angular,Typescript,Post,Http Post,Angular2 Http,我开始学习angular 2,我想在json文件中添加一个新对象,但我总是有以下错误 加载资源失败:服务器响应状态为405(不允许使用方法) 服务类别: @Injectable() export class AppServices { constructor(public http: Http){} headers = new Headers({ 'Content-Type': 'application/json', 'Accept': '

我开始学习angular 2,我想在json文件中添加一个新对象,但我总是有以下错误

加载资源失败:服务器响应状态为405(不允许使用方法)

服务类别:

 @Injectable()
export class AppServices {

    constructor(public http: Http){}

    headers = new Headers({

        'Content-Type': 'application/json',
        'Accept': 'application/json'     
    });

    public getJSON(): Observable<any> {
        return this.http.get("data.json")
            .map((res: any) => res.json())
            .catch((error: any) => console.log(error));
    }

    public add(body: any): Observable<any> {
        let options = new RequestOptions({
            method: RequestMethod.Post,
            headers: this.headers,
            body: JSON.stringify(body),
            url: "data.json"
        });
        return this.http.request(new Request(options))
    }
}
@Injectable()
导出类应用程序服务{
构造函数(公共http:http){}
标题=新标题({
“内容类型”:“应用程序/json”,
“接受”:“应用程序/json”
});
public getJSON():可观察{
返回此.http.get(“data.json”)
.map((res:any)=>res.json())
.catch((错误:any)=>console.log(错误));
}
公共添加(主体:任何):可观察{
让选项=新的请求选项({
方法:RequestMethod.Post,
headers:this.headers,
body:JSON.stringify(body),
url:“data.json”
});
返回此.http.request(新请求(选项))
}
}

方法get不会产生任何错误,问题是用方法POST和PUT,修改文件JSON.

的方法是:您不能对本地JSON文件进行POST请求,它只能处理GET请求……考虑使用NPM:JSON服务器。works@Takler21是的,我喜欢这个包裹,它是如此简单和干净,您不能对本地JSON文件进行POST请求,它只能处理GET请求……考虑使用NPM:JSON服务器。works@Takler21是的,我喜欢这个包装,它是如此简单和干净
 @Injectable()
export class AppServices {

    constructor(public http: Http){}

    headers = new Headers({

        'Content-Type': 'application/json',
        'Accept': 'application/json'     
    });

    public getJSON(): Observable<any> {
        return this.http.get("data.json")
            .map((res: any) => res.json())
            .catch((error: any) => console.log(error));
    }

    public add(body: any): Observable<any> {
        let options = new RequestOptions({
            method: RequestMethod.Post,
            headers: this.headers,
            body: JSON.stringify(body),
            url: "data.json"
        });
        return this.http.request(new Request(options))
    }
}