title: Unexpected end of JSON input - Uncaught SyntaxError
body: <p>everybody. I have an AJAX call which returns me an error mentioned in the title. I believe this is the line which causes error: <code>var obj = jQuery.parseJSON(data);</code> Maybe I wrongly constructed <code>userData</code>.</p>
  
  <p>This is my jQuery:</p>
  
  <pre><code>var userData = 'email=' + email + '&amp;password=' + password; 
  
  $.ajax({
      type: 'POST',
      url: './api/getInfo.php',
      data: userData,
      success: function(data){
          var obj = jQuery.parseJSON(data); 
  
          $('#name').html(obj.firstName + ' ' + obj.lastName);
          ...
      },
      error: function(){
          alert('ERROR');
      }
  });
  </code></pre>
  
  <p>And this is getInfo.php:</p>
  
  <pre><code>if($_SERVER['REQUEST_METHOD'] == 'POST')
  {
       $email = prepareInput($_POST['email']);
       $password = prepareInput($_POST['password']);
  
       $stmt = $connection-&gt;conn-&gt;prepare('SELECT firstName,lastName,... FROM tb_users WHERE email = ? AND password = ?');
       $stmt-&gt;bind_param('ss',$email,$password);
  
       $stmt-&gt;execute();
  
       $result = $stmt-&gt;get_result();
  
       $obj = $result-&gt;fetch_assoc();
  
       echo json_encode($obj);
  }
  </code></pre>
  
  <p>Can someone tell me if I'm doing something wrong?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>function prepareInput($data)
  {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
  
      return $data;
  }
  </code></pre>
  
  <p>Data passed from PHP is empty even if <code>$obj</code> contains values (I checked this by echoing them) so it must be the problem with <code>echo json_encode($obj);</code> statement.</p>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>I finally found the answer - <strong><a href="https://stackoverflow.com/questions/19361282/why-would-json-encode-returns-an-empty-string">Link</a></strong>. It was <strong>encoding problem</strong>. If some strings are not UTF-8 <code>json_encode()</code> will return empty string so to deal with this you need to convert these strings to UTF-8.</p>