title: JQuery $.cookie SyntaxError: Unexpected token o
body: <p>I am using JQuery $.cookie to save my application users token upon their login, then read users token each time they try to access any of the app features using the following code:</p>
  
  <pre><code>        var Token = '';
          var cookieContent = '';
          if(typeof $.cookie('MyAPP') === 'undefined'){
              cookieContent = '';
          }else{
             cookieContent = $.cookie('MyAPP');
             Token = JSON.parse(cookieContent); //Error here
             console.log('Token' + Token);
          }
  </code></pre>
  
  <p>My problem is that first time this code is executed, I get error:</p>
  
  <pre><code>    SyntaxError: Unexpected token o
  </code></pre>
  
  <p>at JSON.parse, if I hit refresh to call this code again, it works normally without any problem. So I am wondering how I can avoid this error? </p>
  
  <p>As far as I understand this error is caused because $.cookie is returning an [Object] which is not parsed by JSON but I am not sure why it is returning object first time the code is executed then work normally 2nd time? Also is there any way I can do to avoid this error? Thanks</p>
  
  <p>Update: Here is how I save the cookie in the first place:</p>
  
  <pre><code> function saveCookie(minutes, Token) {
     var date = new Date();
     var m = minutes;
     date.setTime(date.getTime() + (m * 60 * 1000));
     $.cookie.json = true;
     $.cookie("MyAPP", Token, { expires: date });     
  }
  </code></pre>