Spring 如何使用angular 2获取web服务的内容?
我在后端使用spring框架,在前端使用angular2.4。这是我后端的rest服务Spring 如何使用angular 2获取web服务的内容?,spring,angular,Spring,Angular,我在后端使用spring框架,在前端使用angular2.4。这是我后端的rest服务 @RequestMapping(value="/etreprises" , method = RequestMethod.GET) public List<Entreprise> getAllEntreprise() { return entrepriseDAO.getAllEntreprise(); } 这是浏览者的反应 [{"id":"58db96c945184728e40fa6
@RequestMapping(value="/etreprises" , method = RequestMethod.GET)
public List<Entreprise> getAllEntreprise() {
return entrepriseDAO.getAllEntreprise();
}
这是浏览者的反应
[{"id":"58db96c945184728e40fa6f9","name":"entr1","lagitude":15.3,"latitude":12.2},{"id":"58db96c945184728e40fa6fa","name":"entr2","lagitude":15.3,"latitude":12.2},{"id":"58db96c945184728e40fa6fb","name":"entr3","lagitude":15.3,"latitude":12.2},{"id":"58db96c945184728e40fa6fc","name":"entr4","lagitude":15.3,"latitude":12.2}]
我正在用visualcode处理前端
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
entreprises: Entreprise[];
ngOnInit(): void {
this.http.get('http://localhost:1616/entreprises')
.subscribe(data =>{
console.log(data.json());
this.entreprises=data.json().data;
});
}
constructor(private http: Http) { }
}
以及app.component.html
<h1>
</h1>
<div *ngIf="entreprises">
<div *ngFor ="let x of entreprises">
{{x.name}}
</div>
</div>
但我在浏览器上找不到企业的名称。没有错误的白色页面我不确定,但请尝试以下方法:
export class AppComponent implements OnInit {
entreprises: Entreprise[];
ngOnInit(): void {
this.http.get('http://localhost:1616/entreprises')
.map((response: Response) => response.json())
.subscribe(data =>{
console.log(data);
//this.entreprises=data.json().data;
});
}
constructor(private http: Http) { }
}
什么是console.log(data.json());给你[对象,对象,对象,对象,对象,对象,对象,对象,对象,对象,对象,对象]每个对象包含什么?试试这个console.log(data.json()[0]);数组[20]0:Object id:“58db96c945184728e40fa6f9”滞后:15.3纬度:12.2名称:“entr1”原型:Object 1:Object id:“58db96c945184728e40fa6fa”滞后:15.3纬度:12.2名称:“entr2”原型:Objecttry this
this.enterprises=data.json()代码>在控制台中,它返回以下内容:[对象,对象,对象,对象,对象,对象,对象,对象,对象,对象,对象]
[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
export class AppComponent implements OnInit {
entreprises: Entreprise[];
ngOnInit(): void {
this.http.get('http://localhost:1616/entreprises')
.map((response: Response) => response.json())
.subscribe(data =>{
console.log(data);
//this.entreprises=data.json().data;
});
}
constructor(private http: Http) { }
}