TUrkel

http.service

Oct 10th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { Http, Response, Headers } from '@angular/http';
  3. import 'rxjs/add/operator/map';
  4. import 'rxjs/add/operator/catch';
  5. import { Observable } from 'rxjs/Observable';
  6.  
  7. @Injectable()
  8. export class HttpService {
  9.  
  10.   constructor(private http: Http) { }
  11.  
  12.     getData() {
  13.         return this.http.get('https://angular2-course-ed653.firebaseio.com/title.json')
  14.             .map((response: Response) => response.json());
  15.  
  16.     }
  17.  
  18.     sendData(user: any) {
  19.         const body = JSON.stringify(user);
  20.         const headers = new Headers();
  21.         headers.append('Content-Type', 'application/json');
  22.  
  23.         return this.http.post('https://angular2-course-ed653.firebaseio.com/data.json', body, {
  24.             headers: headers
  25.         }).map((data: Response) => data.json())
  26.             .catch(this.handleError);
  27.     }
  28.  
  29.     getOwnData() {
  30.         return this.http.get('https://angular2-course-ed653.firebaseio.com/data.json')
  31.             .map((response: Response) => response.json());
  32.     }
  33.  
  34.     private handleError (error: any) {
  35.         console.log(error);
  36.         return Observable.throw(error.json());
  37.     }
  38.    
  39.     public getPage(module_link, values = '') {
  40.  
  41.         let config = {
  42.             params: {
  43.                 values,
  44.             },
  45.             withCredentials: true
  46.         }
  47.  
  48.         return this.http.get(module_link, config)
  49.             .map((response: Response) => response.json().base64);
  50.             //.map((response: Response) => atob(response.json().base64));
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment