My post,get and delete request are working fine but i have been struggling on how to make a put request when i submit my form. I am able to fetch data into the form alright but when i click the submit button to edit the data , the update does not reflect in the database. Here is my code below
//form to edit
<form (ngSubmit)="onEdit(edit)" #edit="ngForm" >
<div class="form-group">
<label for="first_name">First Name</label>
<input type="text" class="form-control" id="firstname" [(ngModel)]="edit.first_name" name="first_name" required>
</div>
<div class="form-group">
<label for="last_name">Last Name</label>
<input type="text" class="form-control" id="lastname" [(ngModel)]="edit.last_name" name="last_name" required>
</div>
<button type="submit">Edit</buttton>
//component
edit = {
first_name: "",
last_name: ""
}
onEdit() {
this.httpService.update(this.edit)
.subscribe(
data => console.log(data)
)}
//edit person according to row index
editPerson(person) {
this.edit = person;
console.log(person)
}
// button to edit
<td><a class="btn btn-default" data-target="#edit" (click)="editPerson(client)"><em class="fa fa-pencil"></em></a>Edit</td>
//httpservice
update(person) {
let headers = new Headers({'Content-Type':'application/json'});
let options = new RequestOptions({headers: headers});
let body = JSON.stringify(person);
return this.http.put('http://example.com'+person.id,body,headers)
.map((response:Response) => response.json());