Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. import { Component, OnInit } from '@angular/core';
  2. import { Http, Response, RequestOptions, Headers } from "@angular/http";
  3. import { error } from 'util';
  4. import { User } from '../user-model';
  5.  
  6. @Component({
  7. selector: 'app-http-demo',
  8. templateUrl: './http-demo.component.html',
  9. styleUrls: ['./http-demo.component.css']
  10. })
  11. export class HttpDemoComponent implements OnInit {
  12.  
  13. allUsers: User[] = [];
  14.  
  15. constructor(private http: Http) { }
  16.  
  17. ngOnInit() {
  18. }
  19.  
  20. get() {
  21. let url = 'http://localhost:9000/users';
  22.  
  23. this.http.get(url).subscribe(data => {
  24.  
  25. this.allUsers = data.json() as User[];
  26.  
  27. console.log(this.allUsers);
  28. },
  29. error => {
  30. console.log('failed to load users');
  31. });
  32. }
  33.  
  34. post() {
  35. let url = 'http://localhost:9000/';
  36. let user = { 'id': 10, 'firstName': 'Balram', 'lastName': 'Chavan' };
  37.  
  38. let headers = new Headers({ 'Content-Type': 'application/json' });
  39. headers.append('Accept', 'application/json');
  40.  
  41. let options = new RequestOptions({ headers: headers });
  42.  
  43. this.http.post(url, { user }).subscribe(data => {
  44. console.log(data.json());
  45. console.log('post done');
  46. })
  47. }
  48.  
  49. put() {
  50. let url = 'http://localhost:9000/';
  51. let user = { 'id': 10, 'firstName': 'Balram', 'lastName': 'Chavan' };
  52.  
  53. let headers = new Headers({ 'Content-Type': 'application/json' });
  54. headers.append('Accept', 'application/json');
  55.  
  56. let options = new RequestOptions({ headers: headers });
  57.  
  58. this.http.put(url, { user }, options).subscribe(data => {
  59. console.log(data.json());
  60. console.log('put done');
  61. })
  62. }
  63.  
  64. delete() {
  65. let url = 'http://localhost:9000/290';
  66.  
  67. this.http.delete(url).subscribe(data => {
  68. console.log(data.json());
  69. },
  70. error => {
  71. console.log('failed to delete user');
  72. });
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement