Advertisement
Guest User

Untitled

a guest
Mar 14th, 2016
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Boobies</title>
  6. <script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
  7. </head>
  8. <body>
  9. <section class="login">
  10. <form action=# onsubmit="logInUser(this)">
  11. <input type="text" id="username">
  12. <input type="text" id="password">
  13. <input type="submit">
  14. </form>
  15. <button id="getBooks">Get books BACE!</button>
  16. </section>
  17.  
  18. <section class="books">
  19. <h2>Books</h2>
  20. <ul id="books">
  21.  
  22. </ul>
  23. </section>
  24.  
  25. <script type="text/javascript">
  26.  
  27. var button = $('#getBooks');
  28.  
  29. var logInUser = function() {
  30.  
  31. var APP_ID = "kid_WJRDsntOk-";
  32.  
  33. var user = {username : $("#username").val(), password : $("#password").val()};
  34.  
  35. var authToken = "Basic " + btoa(user.username + ":" + user.password);
  36.  
  37. console.log(user);
  38. console.log(authToken);
  39. $.ajax({
  40. method: "POST",
  41. headers: {
  42. "Authorization": authToken,
  43. },
  44. url: "https://baas.kinvey.com/user/" + APP_ID +"/login",
  45. data: user,
  46. dataType: "json"
  47. }).success(function(data) {
  48. sessionStorage['sessionAuth'] = data._kmd.authtoken;
  49. sessionStorage['userId'] = data._id;
  50. console.log("User logged in!");
  51. console.log(sessionStorage['sessionAuth']);
  52. }).error(function() {
  53. console.log("Ne6to se usra");
  54. });
  55. };
  56.  
  57. button.on('click', function() {
  58.  
  59. var APP_ID = "kid_WJRDsntOk-";
  60. var authToken = "Kinvey " + sessionStorage["sessionAuth"];
  61.  
  62. $.ajax({
  63. method: "GET",
  64. headers: {
  65. "Authorization": authToken
  66. },
  67. url: "https://baas.kinvey.com/apdata/" + APP_ID + "/books",
  68. dataType: "jsonp"
  69. }).success(function(data) {
  70. for (var book in data.result) {
  71. console.log(book);
  72. }
  73. }).error(function() {
  74. console.log("Ne6to se usra");
  75. });
  76. });
  77.  
  78. </script>
  79. </body>
  80. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement