gpt4 book ai didi

Angular 8 NgFor 仅支持绑定(bind)到 Iterables,例如 Arrays error 和 Acces Control Allow origin

转载 作者:行者123 更新时间:2023-12-05 08:22:48 24 4
gpt4 key购买 nike

我有一个模型:

export class Employe {    
constructor(public id?: any,
public nom?: String,
public prenom?: String,
public cin?: String){}
}

employees.component.ts

ngOnInit(){
this.loadEmployes();
}

pageEmployes:any={};

loadEmployes():Observable<any>{
this.http.get("http://localhost:8080/api/employe").subscribe(
data=>{
console.log(data);
this.pageEmployes = data;

}, err=>{
console.log(err);
}
);
return this.pageEmployes;
}

employees.component.html

 <tr *ngFor="let item of pageEmployes">         
<td>{{item.nom}}</td>
<td>{{item.prenom}}</td>
</tr>

CollaborateurController.java

@RestController

@RequestMapping("/api/employe")

@CrossOrigin("*")

public class CollaborateurController {

@Autowired
private CollaborateurRepository collaborateurRepository;

@RequestMapping(value="", method=RequestMethod.GET)
public List<Collaborateur> getEmp() {
return (List<Collaborateur>) collaborateurRepository.findAll();
}

这会引发错误:

ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

Access to XMLHttpRequest at 'http://localhost:8080/api/employe' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

最佳答案

从您的代码示例来看,您正在尝试迭代(循环)一个对象,不是数组。

这部分是错误的:

pageEmployes:any={};

并且应该改为:

pageEmployes: Employe[] = [];

“={};”初始化一个新的对象

“=[];”初始化一个新的可迭代列表

关于Angular 8 NgFor 仅支持绑定(bind)到 Iterables,例如 Arrays error 和 Acces Control Allow origin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58243622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com