index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. $("#btn_login").click(function () {
  2. var url = "login";
  3. var xhr = new XMLHttpRequest();
  4. xhr.open("POST", url);
  5. console.log('url:');
  6. console.log(url);
  7. xhr.setRequestHeader("accept", "application/json");
  8. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  9. xhr.onreadystatechange = function () {
  10. if (xhr.readyState === 4) {
  11. console.log(xhr.status);
  12. responseOBJ = JSON.parse(xhr.responseText) ; // error
  13. console.log(responseOBJ.access_token);
  14. // document.cookie = 'jwt_token='+responseOBJ.jwt_token // access_token -> jwt_token
  15. document.cookie = 'jwt_token='+responseOBJ.access_token;
  16. console.log('cookie: ');
  17. console.log(document.cookie); // test log
  18. console.log('登入成功');
  19. alert('登入成功!!');
  20. window.location.replace("/home");
  21. }
  22. };
  23. var data = "grant_type=&username=" + $('#username').val() + "&password="+$('#password').val()+"&scope=&client_id=&client_secret=";
  24. console.log("data:");
  25. console.log(data); // test
  26. xhr.send(data);
  27. });
  28. // $( ".side-bar-link" ).click(function() {
  29. // console.log(1);
  30. // alert('hi');
  31. // $(this).addClass("active");
  32. // $(".active").siblings().removeClass('active');
  33. // });