SHARE
TWEET

Untitled

a guest Jun 15th, 2017 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(() => {
  2.     var $getToken       = $('#get-token');
  3.     var $getCats        = $('#get-cats');
  4.     var $tableCats      = $('#table-cats');
  5.     var $tableCatsBody  = $('#table-cats__body');
  6.     var token;
  7.  
  8.     function getToken() {
  9.         $.ajax({
  10.             method: 'POST',
  11.             url: 'http://localhost:4000/token',
  12.             data: {
  13.                 username: 'ollie',
  14.                 password: '123'
  15.             }
  16.         })
  17.         .done((response) => {
  18.             token = response.token;
  19.         })
  20.     }
  21.  
  22.     function listCats(index, cat) {
  23.         var tableRow = '';
  24.  
  25.         tableRow += '<tr>';
  26.         tableRow += '<td>' + cat.name + '</td>';
  27.         tableRow += '<td>' + cat.age + '</td>';
  28.         tableRow += '<td>' + cat.type + '</td>';
  29.         tableRow += '<td>' + cat._id + '</td>';
  30.         tableRow += '</tr>';
  31.  
  32.         $tableCatsBody.append(tableRow);
  33.     }
  34.  
  35.     function getCats() {
  36.         $.ajax({
  37.             url: 'http://localhost:4000/cats',
  38.             headers: {
  39.                 'Authorization': 'JWT ' + token
  40.             }
  41.         })
  42.         .done((response) => {
  43.             var cats = response.data;
  44.  
  45.             $tableCatsBody.empty();
  46.  
  47.             $.each(cats, listCats);
  48.  
  49.             $tableCats.removeClass('hidden');
  50.         })
  51.     }
  52.  
  53.     function bindEvents() {
  54.         $getCats.on('click', getCats);
  55.         $getToken.on('click', getToken);
  56.     }
  57.  
  58.     bindEvents();
  59. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top