Advertisement
Guest User

session with localStorage

a guest
Jun 20th, 2012
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         var store = window.localStorage,
  2.     request = {
  3.         url: {SERVER_URL},
  4.         type: "GET",
  5.         dataType: 'json',
  6.         cache: false,
  7.         headers : {
  8.             Cookie: store.getItem('session')
  9.         },
  10.         complete: function (jqXHR, status){
  11.             if (status != 'success') {
  12.                 console.log('ajax status: failure');
  13.             } else if (store.getItem('session') != null) {
  14.                 console.log('ajax status: session exists');
  15.             } else {
  16.                 console.log('ajax status: saving cookie');
  17.                 var header = jqXHR.getAllResponseHeaders();
  18.                 var match = header.match(/(Set-Cookie|set-cookie): (.+?);/);
  19.                 if (match) {
  20.                     session = match[2];
  21.                     store.setItem("session", session);
  22.                 }
  23.             }
  24.         },
  25.         success: function (response){
  26.             if (typeof response != 'object') {
  27.                 console.log('ajax status: no response object');
  28.                 return;
  29.             } else {
  30.                 console.log('ajax status: responding with object');
  31.                 // Do response actions here...
  32.             }
  33.         }
  34.     }
  35.     $.ajax(request);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement