Code: Select all
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.
< /code>
Ich verwende Feder -Boot -Backend und Angular für das Frontend. Und alles ist in Ordnung. :
@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 getAllCandidats() {
try {
List 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);
}
}
}
< /code>
In meinem Backend habe ich diese CORS-Konfiguration, um eine Domäne zu akzeptieren: < /p>
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"
}
};
}
}
Code: Select all
getAllCandidat() {
this._dao.findAllCandidat().subscribe(resp=>{
this.ExcelData = resp
})
}
< /code>
Dies ist mein Service in Angular: < /p>
findAllCandidat() {
return this.http.get(`${BASIC_URL}api/cvs/recrutement`)}