login.ejs 2.62 KB
<!DOCTYPE html>
<html >
  <head>
    <meta charset="UTF-8">
    <title>로그인</title>
    <!--=======Font Open Sans======-->
    <link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
    <!--=======Custom Style======-->
    <link rel="stylesheet" href="css/style.css">

  </head>
  <body>
  <div class="form-box">
    <div class="head">GPS 기반 자유시장 로그인</div>
    <form action="/email/ajax" id="login-form" method="post">
        <div class="form-group">
          <label class="label-control">
            <span class="label-text">Email</span>
          </label>
          <input type="email" name="email" class="form-control" />
        </div>
        <div class="form-group">
          <label class="label-control">
            <span class="label-text">Password</span>
          </label> 
          <input type="password" name="password" class="form-control" />
        </div>
        <input type="submit" value="로그인" class="btn" id ="ajaxsend" />
        <p class="text-p"> <%= message %> </p>
    </form>
  </div>
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
$(window).load(function(){
  $('.form-group input').on('focus blur', function (e) {
      $(this).parents('.form-group').toggleClass('active', (e.type === 'focus' || this.value.length > 0));
  }).trigger('blur');
});
</script>
  <script>

      document.querySelector('#ajaxsend').addEventListener('click', function () {

          var email = document.getElementsByName('email')[0].value
          var password = document.getElementsByName('password')[0].value
          sendAjax('http://localhost:3000/login', {'email':email,'password': password})
      })

      function sendAjax(url, data) {

          data = JSON.stringify(data);
          //alert(data);
          var xhr = new XMLHttpRequest();
          xhr.open('POST', url);
          xhr.setRequestHeader('Content-Type', "application/json");

          xhr.send(data);
          xhr.addEventListener('load', function () {
              console.log(xhr.responseText);
              var result = JSON.parse(xhr.responseText);
              var resultDiv = document.querySelector(".result");
              if(result.email) {
                  //resultDiv.innerHTML = "Welcome, " + result.email + "!!";
                  window.location.href ="/map"
              }
              else window.location.href ="/login"//resultDiv.innerHTML = result;
              //document.querySelector(".result").innerHTML = result.name;
          })
      }
  </script>
</body>
</html>