title: jQuery AJAX Post not writing JSON data to server file
body: <p>I'm new to this language stack...  Trying to understand why the .json file on the server isn't being populated with the data from the ajax post.  Below are two functions.  The first is a jquery function running on the browser, and the other is a php function called by the jquery that runs on the server.  The file permissions for the directory on the server are read-write, /var/www  </p>
  
  <p>There is currently a zero length file named post_Sched.json where the php function should write the json data. </p>
  
  <pre><code>&lt;script&gt;
      function theFunction() {
                     console.log("about to post to post_Sched.php")
          console.log("post_Sched_jstringed = " + post_Sched_jstringed);
  
          request = $.ajax({
              url: "post_Sched.php",
              type: "POST",
              data: { 'data': post_Sched_jstringed },
              //dataType: "JSON",
              success: function (response) {
                  // response from php 
                  $("#jresult").text(JSON.stringify(response));
              },
              error: function (jqXHR, textStatus, errorThrown) {
                  console.log(textStatus, errorThrown);
              }
          });
          console.log("just posted to post_Sched.php")
     }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
          &lt;h2&gt;JSON Post Response: &lt;/h2&gt;
          &lt;div id="jresult"&gt;&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and a php file called post_Sched.php</p>
  
  <pre><code>&lt;?php
    $data = $_POST['data'];
    $file = fopen('post_Sched.json','w');
    fwrite($file, $data);
    fclose($file);
    echo json_encode($data);
  ?&gt;
  </code></pre>
  
  <p>p.s. There is a global variable in the jquery function for the JSON data, and I can see the proper stringify'ed result, so I know the jsonDat is correct:</p>
  
  <pre><code>  var post_Sched_jstringed = JSON.stringify(Sched);
  </code></pre>
  
  <p>Developer tools console shows parsererror SyntaxError: Unexpected token &lt; in JSON at position 0</p>
  
  <p>Can anyone explain the problem and how to fix it?<br>
  Thanks in advance!  </p>