【问题标题】:Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header对预检请求的响应未通过访问控制检查:没有“Access-Control-Allow-Origin”标头
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-04-09
      • 2019-02-02
      • 1970-01-01
      • 2018-02-14
      • 2018-09-16
      • 2020-05-05
      • 1970-01-01
      • 2019-06-28
      • 2018-08-04
      相关资源
      最近更新 更多