script_index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. $("#btn_login").click(function () {
  2. var url = "login";
  3. var xhr = new XMLHttpRequest();
  4. xhr.open("POST", url);
  5. xhr.setRequestHeader("accept", "application/json");
  6. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  7. xhr.onreadystatechange = function () {
  8. if (xhr.readyState === 4) {
  9. responseOBJ = JSON.parse(xhr.responseText)
  10. // document.cookie = 'jwt_token='+responseOBJ.jwt_token // access_token -> jwt_token
  11. document.cookie = 'jwt_token='+responseOBJ.access_token;
  12. alert('登入成功')
  13. if (responseOBJ.access_token!=null)
  14. {
  15. Swal.fire({
  16. title: "登入成功",
  17. icon: 'success',
  18. confirmButtonColor: '#3085d6',
  19. });
  20. }
  21. else{
  22. Swal.fire({
  23. title: "登入失敗",
  24. icon: 'error',
  25. text: responseOBJ.detail,
  26. confirmButtonColor: '#3085d6',
  27. });
  28. }
  29. window.location.replace("/index")
  30. }
  31. };
  32. var data = "grant_type=&username=" + $('#username').val() + "&password="+$('#password').val()+"&scope=&client_id=&client_secret=";
  33. result = xhr.send(data);
  34. console.log(result);
  35. });