1

I am starting to learn angular 2 and I want to add a new object in a json file, but I always have the following errors.

Failed to load resource: the server responded with a status of 405 (Method Not Allowed)

  zone.js:2164 POST http://localhost:50149/src/data.json 405 (Method Not Allowed)
   scheduleTask @   zone.js:2164
   ZoneDelegate.scheduleTask    @   zone.js:384
   onScheduleTask   @   zone.js:274
   ZoneDelegate.scheduleTask    @   zone.js:378
   Zone.scheduleTask    @   zone.js:209
   Zone.scheduleMacroTask   @   zone.js:232
   (anonymous)  @   zone.js:2188
   send @   VM45:3
   (anonymous)  @   http.umd.js:1229
   Observable.subscribe @   Observable.ts:98
   AppComponent.addb    @   app.component.ts:61
   View_AppComponent0.handleEvent_44    @   /AppModule/AppComponent/component.ngfactory.js:366
  (anonymous)   @   platform-browser.umd.js:3221
   ZoneDelegate.invokeTask  @   zone.js:398
   onInvokeTask @   core.umd.js:4426
   ZoneDelegate.invokeTask  @   zone.js:397
   Zone.runTask @   zone.js:165
   ZoneTask.invoke  @   zone.js:460

The service class:

 @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))
    }
}

The method get doesn't give any error, the problem is with methods post and put, the ones that modify the file json.

Takler21
  • 99
  • 1
  • 7

0 Answers0