상세 컨텐츠

본문 제목

ajax, mybatis, HashMap 415 오류

Java TIL

by 개발일지작성 2024. 3. 18. 19:31

본문

728x90
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- jQuery 라이브러리 로드 -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <title>로그인</title>
</head>

<body>
   <h3>로그인</h3>
<%--<form method="post" action="/login">--%>
    <table>
        <tr>
            <th>ID</th>
            <td><input type="text" id="id" value=""/></td>
        </tr>

        <tr>
            <th>password</th>
            <td><input type="password" id="password" value=""/></td>
        </tr>

        <tr>
            <th colspan="2">
                <button onclick="send()">log in</button>
            </th>
        </tr>
    </table>
<%--</form>--%>
</body>
<script>
    function send() {
        $.ajax({
            type: "post",
            url: "/login",
            data: JSON.stringify ({
                id: $("#id").val(),
                password: $("#password").val()
            }),
            contentType:'application/json',
            success: function (data) {
                if (data.result) {
                    // 로그인에 성공한 경우 처리
                    // alert("성공"); // 예: "로그인에 성공했습니다."
                    // 성공 시 리다이렉션
                    window.location.href = "/"; // 또는 다른 주소로 변경
                } else {
                    // 로그인에 실패한 경우 처리
                    alert("로그인에 실패했습니다.");
                }
            },
            error: function (e) {
                console.error("AJAX 요청 실패:", e);
            }
        });
    }
</script>
</html>
@PostMapping("/login")
@ResponseBody
public Map<String, Object> processLogin(@RequestBody Map<String,Object> userData) {
    Map<String, Object> result = userService.isValidUser(userData);
    return result;
}

ajax 로 로그인 정보를 서버에 보내는데 form태크를 사용함으로써 데이터를 2번 보내서 오류 발생(아마도? 잘 몰?루?)

같은 문제이신 분들 form 태그 지워보시길

'Java TIL' 카테고리의 다른 글

인텔리제이 톰캣 404에러  (1) 2024.03.09
인텔리제이 톰캣 한글깨짐 해결방법  (0) 2024.03.09
테스트코드 작성(기초)  (0) 2024.01.24
테스트코드 작성 시 Autowired, Mock이란 무엇인가  (1) 2023.12.28
Schedul  (0) 2023.12.23

관련글 더보기