1

这是我的组件中的方法:

onDelete(s) {
    const conf = confirm('etes vous sur');
    if (conf) {
         console.log(s._links.self.href);
      this.catservice.Deleteprod(s._links.self.href).subscribe(data => {console.log(s._links.self.href);
      }, error1 => {
        console.log(error1);
      });
    }
  }

这是我服务的方法:

  public Deleteprod(url) {
    return this.httpClient.delete(url);
  }

我想删除这个对象,s._links.self.href所以url没有错,但是当我尝试websecurityconfig用我尝试过的她的方法上课时遇到了这个问题,@CrossOrigin(origins = "*")但没有解决方案,请帮助我。

这是我的错误:

Access to XMLHttpRequest at 'http://localhost:8080/offres/24' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

这是我的错误我恢复了对象但我无法删除它

4

3 回答 3

6

在您的注释下方添加以下@RestController注释

@CrossOrigin(origins = "*", allowedHeaders = "*")
于 2020-04-28T09:10:44.043 回答
0

我通过向 SecuityConfig 添加以下代码解决了同样的问题:

http.
   ..... //other code
   .authorizeRequests()
   .antMatchers(HttpMethod.OPTIONS).permitAll()
   ..... //other code
于 2020-10-18T18:09:35.963 回答
0

在网络安全

@Bean
    CorsConfigurationSource corsConfigurationSource() {
        UrlBasedCorsConfigurationSource source = new
                UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
        return source;
    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**")
                .allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")
                .exposedHeaders("Authorization");
    }
于 2020-03-10T10:23:31.650 回答