Serviço do Angular.
import { Injectable, EventEmitter } from '@angular/core';
import { Http, Response } from "@angular/http";
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/throw';
import { BASE_API_URL } from '../base-api-url-defaul';
import { REQUEST_OPTIONS_DEFAULT } from '../request-options.default';
import { AuthenticationService } from '../authentication/authentication.service';
import { Pais } from '../../modelo/endereco/pais.model';
@Injectable()
export class PaisService {
private base: string;
private options: Function;
constructor(
private http: Http,
private authenticationService: AuthenticationService,
) {
this.base = BASE_API_URL + 'paisRecurso';
this.options = REQUEST_OPTIONS_DEFAULT;
}
getPesquisar(pais : Pais) {
let url = this.base + "/buscar";
return this.http.post(url, pais, this.options())
.map((response: Response) => {
console.log("Pesquisou por todos países " + response.status);
return response.json();
}
).catch((error:any) =>
Observable.throw(error.json().error ||
'Erro em buscar países ' + console.log(this.options))
);
}
}
BASE_API_URL, que o serviço do angular chama
export const BASE_API_URL: string = 'http://localhost:8080/des-if-web/admin/';
REQUEST_OPTIONS_DEFAULT, que o serviço do angular chama
import { RequestOptions, Headers } from '@angular/http';
const createDefaultHeaders = function() {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Authorization', 'Bearer ' + localStorage.getItem('token'));
return headers;
};
export const REQUEST_OPTIONS_DEFAULT: Function = createDefaultHeaders;