title: Getting data as JSON from a PHP file on the server
body: <p>I am using <code>AJAX</code> call to request the <code>PHP</code> file from the server. The <code>PHP</code> file is holding some objects. <code>JSON.parse()</code> is being used to convert the result into a <code>JavaScript</code> object. So the problem is after running the program I see the following error in the browser console:</p>
  
  <pre><code>Uncaught SyntaxError: Unexpected end of JSON input
  at JSON.parse (&lt;anonymous&gt;)
  at XMLHttpRequest.xmlhttp.onreadystatechange (index.php:15)
  </code></pre>
  
  <p>I have no idea why the program is not working, although my Apache server, and PHP are running fine.
  The program I am working on:</p>
  
  <p>index.php:</p>
  
  <pre><code>    &lt;p id="demo"&gt;&lt;/p&gt;
  
  &lt;script&gt;
  
  var xmlhttp = new XMLHttpRequest();
  
  xmlhttp.onreadystatechange = function() {
      if (this.readyState == 4 &amp;&amp; this.status == 200) {
          myObj = JSON.parse(this.responseText);
          document.getElementById("demo").innerHTML = myObj.name;
      }
  };
  xmlhttp.open("GET", "demo_file.php", true);
  xmlhttp.send();
  
  &lt;/script&gt;
  </code></pre>
  
  <p>demo_file.php:</p>
  
  <pre><code>    &lt;?php
  $myObj-&gt;name = "John";
  $myObj-&gt;age = 30;
  $myObj-&gt;city = "New York";
  </code></pre>