我正在使用Angle 7(前端)和假服务器json(后端)设置应用程序,我想将用户的详细信息(Firstname,Role,LoginTime,连接持续时间)发送到我的假服务器json!但这行不通!服务器json始终为空!
details.ts:
import {AuthenticationService} from '../_services';
import { User } from '../_models';
export class Details
{
authenticationService : AuthenticationService;
currentUser = this.authenticationService.currentUserValue;
role = this.currentUser.role;
logintime = this.authenticationService.today;
duree = this.authenticationService.timeLeft;
}
details.service.ts:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Employee } from '../shared/employee';
import { Observable, throwError } from 'rxjs';
import { retry, catchError } from 'rxjs/operators';
import {Details} from './details';
@Injectable({
providedIn: 'root'
})
export class DetailsService {
apiURL = 'http://localhost:3000';
constructor(private http: HttpClient) { }
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
}
createEmployee(details): Observable<Details> {
return this.http.post<Details>(this.apiURL + '/details',
JSON.stringify(details), this.httpOptions)
.pipe(
retry(1),
catchError(this.handleError)
)
}
getEmployees(): Observable<Details> {
return this.http.get<Details>(this.apiURL + '/details')
.pipe(
retry(1),
catchError(this.handleError)
)
}
handleError(error) {
let errorMessage = '';
if(error.error instanceof ErrorEvent) {
// Get client-side error
errorMessage = error.error.message;
} else {
// Get server-side error
errorMessage = `Error Code: ${error.status}\nMessage:
${error.message}`;
}
window.alert(errorMessage);
return throwError(errorMessage);
}
}
我希望在伪造的json服务器中看到用户的详细信息,但是它是空的!感谢您的帮助
答案 0 :(得分:0)
我的假设是假服务器从http://localhost:3000/details返回用户数据
如果您的服务器发送了这样的数据。
{
name:'some name',
role:'admin'
}
您可以创建类似的详细信息
export class Details{
name:string;
role:string
}
您可以像
一样消费this.detailsService.getEmployees().subscribe(value => console.log(value));