I am building an Angular 2 app and I have created an API that is online and that lets me update a user that has name, email and about fields. I tested it with Rest Easy and I could update any user but when I try to update it from Angular 2 nothing changes in database, what I am trying to do is the following:
updateUser(user: User) {
let url = 'http://example.com/api/user/update' + user.user_id;
console.log(JSON.stringify(user)); // this prints out in console correctly
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http
.put(url, JSON.stringify(user), { headers: headers })
.map(res => res.json());
}
The full user.service.ts file
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from "@angular/http";
import { Observable } from "rxjs/Rx";
import 'rxjs/add/operator/map';
@Injectable()
export class UserService {
constructor(private http: Http) {
console.log('user service initialized');
}
// this works perfectly
getUsers() {
return this.http.get('http://example.com/api/users')
.map(res => res.json());
}
// this also works perfectly
getUser(userId: number) {
return this.http.get('http://example.com/api/user/' + userId)
.map(res => res.json());
}
// this is where nothing happens, only prints in console the object
// but no errors are displayed
updateUser(user: User) {
let url = 'http://henriquealho.esy.es/public/api/user/update/' + user.user_id;
console.log(JSON.stringify(user)); // this prints out in console correctly
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http
.put(url, JSON.stringify(user), { headers: headers })
.map(res => res.json());
}
}
interface User {
user_id: number,
name: string,
email: string,
about: string
}
Anyone knows what I am doing wrong? Thank you!