Access to XMLHttpRequest at 'http://127.0.0.1:8082/api/cvs/recrutement' from origin...

Discussão em 'Angular' iniciado por King Kapeta, Fevereiro 19, 2025.

  1. King Kapeta

    King Kapeta Guest

    can you help me to fix it,

    Access to XMLHttpRequest at 'http://127.0.0.1:8082/api/cvs/recrutement' 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.

    I use Spring boot backend end angular for froont end.

    when i call My API spring boot with Postman, i dont have a problem everthing is ok, when i Call My API with angular App, i always get this problem of has been blocked by CORS policy

    1. this My Controller

    @RestController
    @CrossOrigin(origins = "http://localhost:4200")
    public class CSVController {

    @Autowired CSVService fileService;
    @Autowired CandidatRepository dao;


    @GetMapping("/api/cvs/recrutement")
    @PreAuthorize("hasRole('RH') or hasRole('FINANCE') or hasRole('ADMIN')")
    public ResponseEntity<List<Candidat>> getAllCandidats() {
    try {
    List<Candidat> Candidats = fileService.getAllTutorials();

    if (Candidats.isEmpty()) {
    return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    return new ResponseEntity<>(Candidats, HttpStatus.OK);
    } catch (Exception e) {
    return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
    }
    }
    }


    in my Back end, i have this config to accpter any domaine

    public class CorsConfig implements WebMvcConfigurer{


    @Bean
    public WebMvcConfigurer corsConfigurer() {
    System.out.println("CORS Config success");
    return new WebMvcConfigurer() {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**").allowedOrigins("http://localhost:4200"); // "/create-cars"
    }
    };

    }
    }


    this My component.ts in angular

    getAllCandidat() {
    this._dao.findAllCandidat().subscribe(resp=>{
    this.ExcelData = resp
    })
    }


    This My service in angular

    findAllCandidat() {
    return this.http.get<any[]>(`${BASIC_URL}api/cvs/recrutement`)}

    Continue reading...

Compartilhe esta Página