
$("#btn_login").click(function () {
  var url = "login";
  var xhr = new XMLHttpRequest();
  xhr.open("POST", url);
  xhr.setRequestHeader("accept", "application/json");
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {
      responseOBJ = JSON.parse(xhr.responseText)
      // document.cookie = 'jwt_token='+responseOBJ.jwt_token    // access_token -> jwt_token
      document.cookie = 'jwt_token='+responseOBJ.access_token;
      if (responseOBJ.access_token!=null)
      {
        Swal.fire({
          title: "登入成功",
          icon: 'success',
          confirmButtonColor: '#3085d6',
        },function(isConfirm){
          if(isConfirm){window.location.replace("/index");}
        }
        );
      }
      else{
        Swal.fire({
          title: "登入失敗",
          icon: 'error',
          text: responseOBJ.detail,
          confirmButtonColor: '#3085d6',
        });
      }
      
    }
  };
  var data = "grant_type=&username=" + $('#username').val() + "&password="+$('#password').val()+"&scope=&client_id=&client_secret=";
  result = xhr.send(data);
  console.log(result);
});

