본문 바로가기

devlog/Javascript

jQuery ajax POST 405 오류에 관한 해결 방법

개발환경

  • thymeleaf 3.0.11

  • Spring Boot 2.2.5

  • IntelliJ Ultimate 2020.1

  • JDK 8

  • jQuery 2.1.4

  • Gradle

  • Webpack

발생

spring security 사용 중 ajax post 방식으로 호출 시 405 에러 발생

오류 stack strace

POST http://localhost:8080/user/checkDuplicate 405

원인

spring security 의 _csrf_header만 넘기고 _csrf 값을 넘기지 않음

해결

AS-IS

        beforeSend: function (xhr) {
          xhr.setRequestHeader($("meta[name='_csrf_header']").attr("content"));
        }

TO-BE

        beforeSend: function (xhr) {
          xhr.setRequestHeader($("meta[name='_csrf_header']").attr("content"),
              $("meta[name='_csrf']").attr("content"));
        }

공부하면서 적은 내용이라 맞지 않는 부분이 있을 수 있습니다. 피드백 주시면 공부하여 반영하겠습니다.