Zugriff auf xmlhttprequest unter 'http://127.0.0.1:8082/api/cvs/recrutement' from Origin 'http: // localhost: 4200' wurdJava

Java-Forum
Guest
 Zugriff auf xmlhttprequest unter 'http://127.0.0.1:8082/api/cvs/recrutement' from Origin 'http: // localhost: 4200' wurd

Post by Guest »

Kannst du mir helfen, es zu beheben, < /p>

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.

Ich verwende Feder Boot Backend End Angular für das Ende. Ein Problem, das alles in Ordnung ist,
Wenn ich meine API mit angularer App aufrufe, erhalte ich immer, dass dieses Problem durch die CORS -Richtlinie blockiert wurde. Mein Controller < /li>
< /ol>

Code: Select all

 @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 hinteren Ende habe ich diese Konfiguration, um eine Domaine zu accpter < /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>
Dies meine Komponente.ts in Angular < /p>
getAllCandidat() {
this._dao.findAllCandidat().subscribe(resp=>{
this.ExcelData = resp
})
}
< /code>
Dies mein Dienst in Angular < /p>
  findAllCandidat() {
return this.http.get(`${BASIC_URL}api/cvs/recrutement`)}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post