我有一个rest服务,它是在Spring引导应用程序中创建的。
我使用Angular 6并将其称为REST服务。
当我将http请求发送到Rest服务时,该rest服务被调用两次。
我该如何解决这个问题?
以下是我的服务客户端代码。
import {Component, OnInit} from '@angular/core';
import {Payment} from './Payment';
import {HttpClient, HttpHeaders, HttpResponse} from '@angular/common/http';
import {Token} from './Token';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
title = 'Home';
token: Token;
payments: Payment[];
jwt = '';
constructor(private http: HttpClient) {
}
ngOnInit() {
this.getToken();
console.log('dsdsd=>' + this.jwt);
}
getToken() {
this.http.post<Token>('http://localhost:8084/login', JSON.stringify({ username: 'mehman', password: 'mehman' })
)
.subscribe(res => {
this.token = res.token;
this.getPayments(res.token);
});
}
getPayments(token: Token) {
console.log(token);
const headers = new HttpHeaders().set('Content-Type', 'application/json')
.set('Authorization', '' + token);
this.http.get<Payment[]>('http://localhost:8084/payment/all', { headers: headers}
)
.subscribe(res => {
this.payments = res;
console.log(res);
});
}
}