对预检请求的响应未通过访问控制检查:无'Access-Control-Allow-Origin'标头



当我尝试上传文件并将其放入数据库中时,我收到此错误 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);
})
}

这是我的组件。

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 作为客户端并且有效. 但是当我想要棱角分明时。 这似乎并不容易..有人能帮我吗 谢谢

您可以添加:@CrossOrigin("*"(

@RestController
@RequestMapping("api")
@CrossOrigin("*")
public class Service () {
// Code
}

相关内容

最新更新