title: AJAX json unexpected token '
body: <p>I have this code:</p>
  
  <pre><code>    $.ajax({
              dataType: 'text',
              url: '/_/js/answers.json',
              type: "GET",
              success: function (data) {
                  alert(data);
                  alert(data.code);
                  var result = JSON.parse(data);
                  var hey = JSON.parse('{"code": 123}'); 
                  alert(hey.code);
                  alert(result.code);
              },
              error: function () {
                  alert("code not found");
              }
          });
  </code></pre>
  
  <p>In the first alert, <code>alert(data)</code> it shows me '{"code": 123}', in the second alert <code>alert(data.code)</code>, it tells me <code>undefined</code>, in the third alert <code>alert(hey.code)</code>, it shows me <code>123</code>, and that's what I want, but in the fourth alert, the console tells me <code>Uncaught SyntaxError: Unexpected token '</code>.<br> 
  When I change the <code>JSON.parse</code> to <code>$.parseJSON</code>, it does exactly the same things.<br>
  I don't know what's wrong, the json is fine (exactly the same as the json in var hey). <br><br>
  I passed the json to the server like this: 
  <strong>javascript:</strong> <br></p>
  
  <pre><code>var json = {code: code};
          json = JSON.stringify(json);
          json = {data: json};
  
          $.ajax({
              url: "/_/js/write-json.php",
              type: "POST",
              dataType: 'json',
              data: json
          }); 
  </code></pre>
  
  <p><strong>php:</strong><br></p>
  
  <pre><code>    &lt;?php
      $myFile = "answers.json";
      $fh = fopen($myFile, 'w') or die("can't open file");
      fwrite($fh,var_export($_POST['data'], true));
      fclose($fh);
      ?&gt;
  </code></pre>
  
  <p>Thanks, bhc11.</p>