title: Storing JSON data in jQuery cookie after successful AJAX call
body: <p>I am trying to store JSON data I am retrieving via AJAX in a jQuery cookie. I am using the following plugin: <a href="https://github.com/carhartl/jquery-cookie" rel="nofollow noreferrer">https://github.com/carhartl/jquery-cookie</a> to achieve this. The plugin works great when I am storing non JSON objects.</p>
  <p>I can see it is working perfectly when storing simple strings. However, when I try to store my JSON data that I get from my AJAX call, the cookie is not saving. Here is my relevant code:</p>
  <pre><code>        $.ajax({
              dataType: &quot;json&quot;,
              url: &quot;http://www.example.com/sandbox/BF/context/messagesNew.php&quot;,
              success: function(data) {
              
                  //This works
                  $.cookie('Name', 'John');
                  console.log($.cookie('Name'));
                
                  //This does not work
                  $.cookie(&quot;test-data&quot;, JSON.stringify(data));
                  var myJSONCookie = JSON.parse($.cookie(&quot;test-data&quot;));
                  console.log(myJSONCookie); // Returns error in console: SyntaxError: JSON Parse error: Unexpected identifier &quot;undefined&quot;
              }
          });
  </code></pre>
  <p>I have tried several other snippets of code but none have worked properly. I do not even think that <code>test-data</code> is being created as a cookie because when I logged the entire <code>$.cookie();</code> to the console, my cookie called <code>Name</code> appears but <code>test-data</code> does not.</p>