개요
ajax를 통해 Date 타입을 RequestParam으로 보내는데 아래와 같이 400 에러가 발생했습니다.
HTTP Status 400: The request sent by the client was syntactically incorrect.
코드
html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form id="pageForm" method="get"> | |
<!-- 중략 --> | |
<input type="date" id="from" name="from" value="${searchCondition.getFrom()}"/> | |
- | |
<input type="date" id="to" name="to" value="${searchCondition.getTo()}"> | |
</form> | |
<script type="text/javascript">; | |
function fnListPage(pageNo) { | |
$.ajax({ | |
url: "/listAjax.do", | |
method: "get", | |
data: $("#pageForm").serialize(), | |
dataType: "html", | |
success: function (data) { | |
$("#ajaxPage").html(data); | |
}, | |
error: function (xhr, status, error) { | |
alert(xhr.status + " : 서버와의 통신이 원활하지 않습니다. 다시 시도해 주십시오."); | |
} | |
}); | |
} | |
</script> |
Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GetMapping(value = "/listAjax.do") | |
public ModelAndView listAjax( | |
@RequestParam("from") LocalDate from, | |
@Requestparam("to") LocalDate to) { | |
// 생략 | |
} |
해결 방법
아래와 같이 @DateTimeFormat 어노테이션을 추가하면 해결되는 문제입니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GetMapping(value = "/listAjax.do") | |
public ModelAndView listAjax( | |
@RequestParam("from") @DateTimeFormat(pattern="yyyy-MM-dd") LocalDate from, | |
@Requestparam("to") @DateTimeFormat(pattern="yyyy-MM-dd")LocalDate to) { | |
// 생략 | |
} |
참고
How to accept Date params in a GET request to Spring MVC Controller?
I've a GET request that sends a date in YYYY-MM-DD format to a Spring Controller. The controller code is as follows: @RequestMapping(value="/fetch" , method=RequestMethod.GET) public @Response...
stackoverflow.com
반응형
'[DEV] 기록' 카테고리의 다른 글
[SpringBoot + jQuery] 파일 다운로드 기능 구현 (0) | 2022.04.08 |
---|---|
[MySQL] 날짜 차이 구하는 함수 (0) | 2022.04.05 |
[jQuery] $.ajax is not a function (0) | 2022.04.03 |
[javascript] sleep 함수 구현 (0) | 2022.03.24 |
[javascript] json 이쁘게 출력하기 (2) | 2022.03.24 |