title: how to pass JSON data to php using ajax post
body: <p>Here is the code: (the #debug div text is shown below)</p>
  
  <pre><code>$("#debug").text(JSON.stringify(data));
  // Try to save to a file
  $.ajax({
      type: 'POST',
      url: './json.php',
      dataType: 'json',
      data: JSON.stringify(data),            
      success: function(xhr, status, errorMessage) {
          if( xhr.responseText == "Success" ) {
              alert("Success!");
          } else {
              alert("response was "+xhr.responseText);
          }
      },
      error: function(xhr, status, errorMessage) {
          $("#debug").append("RESPONSE: "+xhr.responseText+", error: "+errorMessage);
      }
  });
  </code></pre>
  
  <p>The JSON.php page is:</p>
  
  <pre><code>&lt;?php
  openlog("scriptLog.txt", LOG_PID | LOG_PERROR, LOG_LOCAL0);
     $json = $_POST['json'];
  // open syslog, include the process ID and also send
  // the log to standard error, and use a user defined
  // logging mechanism
      syslog(LOG_DEBUG, "Received Data");
  
     if (json_decode($json) != null) { /* sanity check */
      $file = fopen('./data.json','w+');
       fwrite($file, json_decode($json));
       fclose($file);
     } else {
       syslog(LOG_DEBUG,"Failure");
       return "Failure, json decode is null";
     }
     closelog();
     return "Success";
  ?&gt;
  </code></pre>
  
  <p>In the log I get:</p>
  
  <p>Mar 14 14:50:54  scriptLog.txt[21902] : Received Data</p>
  
  <p>Mar 14 14:50:54  scriptLog.txt[21902] : Failure</p>
  
  <p>In the debug div text I get:</p>
  
  <pre><code>{"1457981454959":{"id":1457981454959,"code":"1","title":"Test","date":"22/03/2016","description":"a Task"}}RESPONSE: , error: SyntaxError: JSON Parse error: Unexpected EOF
  </code></pre>
  
  <p>1)  How can I examine the posted data? I.e. "Received Data: "+WHAT in syslog to see its structure.
  2)  JSON parse error?  Most examples I see use this stringify function.  then they use json_decode to get the values.  Why the parse error?</p>