form.html 2.1 KB
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mukho - register form</title>
</head>
<body>
    <form action="/register/form" method="post">
        <h2>Hello. It's me mukho.</h2>

        <a href="http://localhost:3000/">main</a>
        <a href="http://localhost:3000/about">about</a>
        <a href="http://localhost:3000/info">info</a><br/>

        id : <input type="text" name="ID" required minlength='1' maxlength='20' placeholder="ID"><br/>
        password : <input type="password" name="password" required minlength='1' maxlength='20' placeholder="Password"><br/> <!-- type password는 입력 값을 *로 표시함-->
        <input type="submit">
    </form>

    <button class="ajaxsend">AJAXsend</button>

    <div class="result"></div>

    <script>
        document.querySelector('.ajaxsend').addEventListener('click', function(){
            var inputID = document.forms[0].elements[0].value;
            var inputPW = document.forms[0].elements[1].value;
            var inputdata = {'ID' : inputID , 'password' : inputPW};
            sendAjax('http://localhost:3000/register/login', inputdata);
        })

        function sendAjax(url, data){
            var data = {'ID' : data.ID, 'password' : data.password};
            data = JSON.stringify(data);

            var xhr = new XMLHttpRequest();
            xhr.open('POST', url);
            xhr.setRequestHeader('Content-Type', "application/json"); // 서버로 보낼 때 json type으로 보내겠다.
            xhr.send(data);

            xhr.addEventListener('load', function(){
                var result = JSON.parse(xhr.responseText);
                var resultDiv = document.querySelector(".result");
                // 웹 반응
                if(result.result == "ok") resultDiv.innerHTML = result.ID;
                else if(result.result == 'pw err') resultDiv.innerHTML = "password error";
                else resultDiv.innerHTML = "";
            });
        };
    </script>
</body>
</html>