Java 不显示从http响应接收的数据
我有一个服务类型脚本类,如下所示:Java 不显示从http响应接收的数据,java,html,angular,typescript,spring-boot,Java,Html,Angular,Typescript,Spring Boot,我有一个服务类型脚本类,如下所示: export class HomeApiService{ apiURL = 'http://localhost:8080/api'; constructor(private http: HttpClient) {} getProfileData():Observable<HomeModelInterface[]>{ return this.http.get<HomeModelInterface[]>(this.ap
export class HomeApiService{
apiURL = 'http://localhost:8080/api';
constructor(private http: HttpClient) {}
getProfileData():Observable<HomeModelInterface[]>{
return this.http.get<HomeModelInterface[]>(this.apiURL+'/home');}
}
export class HomeComponent implements OnInit {
public profileData: HomeModelInterface[] | undefined ;
constructor(private homeApiService:HomeApiService) { }
ngOnInit() {
this.homeApiService.getProfileData().subscribe(data=> {
this.profileData = data;
console.log(this.profileData);
});
}
<div *ngFor="let profile of profileData">
<h1>{{profile.data}}</h1>
</div>
我有如下html文件:
export class HomeApiService{
apiURL = 'http://localhost:8080/api';
constructor(private http: HttpClient) {}
getProfileData():Observable<HomeModelInterface[]>{
return this.http.get<HomeModelInterface[]>(this.apiURL+'/home');}
}
export class HomeComponent implements OnInit {
public profileData: HomeModelInterface[] | undefined ;
constructor(private homeApiService:HomeApiService) { }
ngOnInit() {
this.homeApiService.getProfileData().subscribe(data=> {
this.profileData = data;
console.log(this.profileData);
});
}
<div *ngFor="let profile of profileData">
<h1>{{profile.data}}</h1>
</div>
以下是我的ResponseHomeJava类:
public class ResponseHome {
private String data[];
private String errors[];
public String[] getData() {
return data;
}
public void setData(String[] data) {
this.data = data;
}
public String[] getErrors() {
return errors;
}
public void setErrors(String[] errors) {
this.errors = errors;
}
}
下面是我的前端模型typescript类,用于将json解析为模型类:
export interface HomeModelInterface{
data: string[];
errors: string[];
}
运行chrome时,我看到以下错误:
找不到类型为“object”的不同支持对象“[object]”
您能提供帮助吗?响应是一个单一实体,一个对象,您正试图在其上运行for循环。如果您查看服务类我正在返回HomeModelInterface getProfileData()的数组,则需要直接使用它。
{{profileData.data}
:可观察的{return this.http.get(this.apirl+'/home');}您的响应返回带有数组的HomeModelInterface
的单个实例。,因此,不要使用HomeModelInterface[]
,而是使用HomeModelInterface
并循环使用model.data[]
。