title: Ajax call returns SyntaxError: Unexpected end of input
body: <p>I am working on a very simple notepad application.
  The user may fetch "a file" (in reality it is just a row from MySQL) to edit it.<br>
  When submitting the form directly to the php file ( just remove e.preventDetfault ), the output is the json encoded output I want:</p>
  
  <pre><code>{"data":{"filename":"testfile","content":"this is the file content","lastupdate":"2016-03-06 13:13:30"}}
  </code></pre>
  
  <p>However, when ran through the below Ajax call, it always returns <strong>SyntaxError: Unexpected end of input.</strong></p>
  
  <p>The form</p>
  
  <pre><code>&lt;form id="openfile" action="backend.php" method="POST"&gt;
  &lt;input type="text" name="filename" placeholder="Enter filename to open" id="filename" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>The jQuery AJAX call</p>
  
  <pre><code>$("#openfile").submit(function(e) {
  
    var filename = $("#filename").val();
  
    openFile(filename);
  
    e.preventDefault(); 
  
  });
  
  
  function openFile(filename) {
  
    if(filename == "") {
      return false;
    }
  
  
    $.ajax({
           type: "POST",
           url: "backend.php",
           data: {action: "load", file: filename},
           success: function(response) {
           console.log(response);
           },
           error: function(XMLHttpRequest, textStatus, errorThrown) {
              console.log("Status: " + textStatus);
              console.log("Error: " + errorThrown);
          }
         });
  
  }
  </code></pre>
  
  <p>The backend</p>
  
  <pre><code>      try {
  
          $db = new PDO(DB_DSN, DB_USER, DB_PASS);
          $sql = "SELECT `filename`, `content`, `lastupdate`
                  FROM `notepad`
                  WHERE `filename` = :filename
                  LIMIT 1";
          $sth = $db-&gt;prepare($sql);
          $sth-&gt;bindParam(":filename", $_POST['file'], PDO::PARAM_STR);
          $sth-&gt;execute();
  
          if($sth-&gt;rowCount() != 0) {
            $result = $sth-&gt;fetch(PDO::FETCH_ASSOC);
             $response['data'] = $result;
            //echo json_encode($result);
          } else {
            $response['error'] = "File does not exist";
          }
  
        } catch(PDOException $e) {
          $response['error'] = $e-&gt;getMessage();
        }
  
        // Return json data
        echo json_encode($response);
  </code></pre>
  
  <p>I know for sure the correct data is being sent (chrome dev tools -> network).</p>
  
  <p>Using a nearly identical ajax call to save a file works just fine.</p>