I need some help with this. I'm Building a Library app .I need to get a list of books, titles, and authors from the server and be able to update and insert new books into the data base on the server. The Get Method works fine and i get a list of all the information but the pust method doesnt work due to this ERROR.
POST http://localhost:3002/app/components/bookdata/dbooks.json 404 (Not
Found)
import {Injectable} from '@angular/core';
import {Http,Response} from '@angular/http';
import {Headers} from '@angular/http';
import 'rxjs/add/operator/map';
import {RequestOptions} from '@angular/http';
@Injectable()
export class BookService {
public _url:string="app/components/bookdata/dbooks.json";
constructor(private _http:Http){}
getBooks(){
return this._http.get(this._url)
.map((res:Response)=>res.json());
}
insertNewBook(book:Book){
let body=JSON.stringify(book);
let headers=new Headers({'Content-Type':'application/json'});
let options=new RequestOptions({headers:headers});
this._http.post(this._url,body).subscribe();
// .map((res:Response)=>res.json());
}
}
export interface Book{
id:number;
Author:string;
Date:string;
Title:string;
}
AND THATS THE WAY I SEND IT
import{Component,OnInit} from '@angular/core';
import {Book} from '../services/book.service'
import {Injectable} from '@angular/core';
import {Http,Response} from '@angular/http';
import {BookService} from '../services/book.service';
import 'rxjs/add/operator/map';
@Component({
selector:'newBook',
template:`
<div>
<div>
<label for="Author">Author:</label>
<input type="text" id="au" #au>
</div>
<div>
</div>
<div>
<label for="Date">Date:</label>
<input type="text" id="da" #da>
</div>
<div>
<label for="Titl">Title:</label>
<input type="text" id="ti" #ti>
</div>
<button (click)="InsertData(au.value,da.value,ti.value)">Create</button>
</div>
`
})
export class NewBookComponent {
book:Book;
static num:number;
constructor(private _bookService:BookService){
NewBookComponent.num=10;
}
InsertData(au:string,da:string,ti:string){
let book:Book={id:NewBookComponent.num++,Author:au,Date:da,Title:ti};
this._bookService.insertNewBook(this.book);
//.subscribe(res=>console.log(res));
}
}