我在後端使用spring框架,在frontEnd使用angular2.4。這是我的後端如何獲得角度2的Web服務的內容?
@RequestMapping(value="/etreprises" , method = RequestMethod.GET)
public List<Entreprise> getAllEntreprise() {
return entrepriseDAO.getAllEntreprise();
}
的其他服務,我添加CorsFilter允許愨visualcode通信和STS
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true); // you USUALLY want this
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
,和本從布勞爾響應
[{"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}]
和我正在與視覺代碼前端
@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>
,這是檢查元素
[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
,但我不能讓在其瀏覽器的企業經營者的名稱的響應。沒有錯誤
什麼的console.log(data.json());給你 –
[對象,對象,對象,對象,對象,對象,對象,對象,對象,對象] –
每個對象包含什麼?試試這個console.log(data.json()[0]); –