에러문장
Access to XMLHttpRequest at 'http://localhost:8080/api/boards' 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.
해결방법
백엔드서버에서 해주는 응답에 다음헤더 넣어주기
HttpHeaders headers = new HttpHeaders();
headers.set("Access-Control-Allow-Origin", "*");
headers.set("Access-Control-Allow-Methods","GET,POST,OPTIONS,DELETE,PUT");
headers.set("Access-Control-Allow-Headers", "Origin,Accept,X-Requested-With,Content-Type,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization");
@GetMapping("/")
public ResponseEntity<?> findAll() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("text","xml", Charset.forName("UTF-8")));
headers.set("Access-Control-Allow-Origin", "*");
headers.set("Access-Control-Allow-Methods","GET,POST,OPTIONS,DELETE,PUT");
return ResponseEntity.status(HttpStatus.OK).headers(headers).body("응답이에요");
}
또는
백엔드의 Controller에 다음과 의 어노테이션으로 프론트 서버 정보 입력해주기.
@CrossOrigin(origins = "http://localhost:4200")
@RestController
public class BoardController {
@CrossOrigin(origins = "http://localhost:4200")
@GetMapping("/boards")
public ResponseEntity<?> findAll() throws Exception {
return ResponseEntity.status(HttpStatus.OK).body(listBoard);
}
}
'Spring' 카테고리의 다른 글
Lombok 설정방법 ( Gradle / Intellij) (0) | 2021.01.05 |
---|---|
Spring 콘솔에 내용 출력하는 방법? (0) | 2020.08.04 |
Annotation 으로 AOP 설정하기 (0) | 2020.07.20 |
(IntelliJ 에러)Unknown run configuration type SpringBootApplicationConfigurationType (0) | 2020.07.10 |
페이징 처리하기 Angular With Spring (0) | 2020.07.08 |