【发布时间】:2018-10-12 11:10:36
【问题描述】:
当我尝试使用 angular4 作为前端上传文件并将其放入数据库时遇到此错误
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 403.
这是我的服务
upload(fichier) {
return this._http.post(this._urlbase+'upload',{"file":fichier} ,
{ headers : new HttpHeaders().set('Content-Type', 'multipart/form-data' ). append('Access-Control-Allow-Origin','*')}
) .subscribe(response => {
this.getAllFichiers();
this.snackBar.open('le fichier est bien ajouté', 'Annuler', {
duration: 2000,
});
}, error => {
console.error('erreur de l\'ajout ', error);
})
}
这是我的 component.ts
upload() {
let fichier=this.fileInput.nativeElement.files[0];
console.log(fichier);
const formdata=new FormData();
formdata.append('file',fichier);
console.log(formdata);
this.fileManagerService.upload(fichier);}
对于我使用 Spring Boot 的后端,我添加了跨域以允许访问:
@CrossOrigin(exposedHeaders="Access-Control-Allow-Origin")
我尝试使用 ARc 作为客户端并且效果很好。但是当我想要有角度的时候。这似乎并不容易..有人可以帮助我吗 谢谢你
【问题讨论】:
标签: angular spring-boot