title: ajax json javascript
body: <p>So in trying to have javascript pass values to a php script using Ajax i get the following error message.</p>
  
  <p>Uncaught SyntaxError: Unexpected end of input</p>
  
  <p>In stepping through the code I found saw that my response from my script came back a null string. Would someone please point me to my mistake, I cannot seem to get a grasp on it.</p>
  
  <p>Here is my javascript (request.js)</p>
  
  <pre><code>var request;
  
  function getHTTPObject()
  {
      var xhr = false;
      if (window.XMLHttpRequest)
      {
          xhr = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
          try
          {
              xhr = new ActiveXObject("Msxml2.XMLHTTP");
          }
          catch(e)
          {
              try
              {
                  xhr = new ActiveXObject("Microsoft.XMLHTTP");
              }
              catch(e)
              {
                  xhr = false;
              }
          }
      }
      return xhr;
  }
  
  function runAjax(JSONstring)
  {
      // function returns "AJAX" object, depending on web browser
      // this is not native JS function!
      request = getHTTPObject();
      request.onreadystatechange = sendData;
      request.open("GET", "request.php?json="+JSONstring, true);
      request.send(null);
  }
  
  // function is executed when var request state changes
  function sendData()
  {
      // if request object received response
      if(request.readyState == 4)
      {
      // parser.php response
      var JSONtext = request.responseText;
  
      // convert received string to JavaScript object
      var JSONobject = JSON.parse(JSONtext);
  
  
      // notice how variables are used
      var msg = "Number of errors: "+JSONobject.errorsNum+
          "\n- "+JSONobject.error[0]+
          "\n- "+JSONobject.error[1];
  
      alert(msg);
      }
  }
  </code></pre>
  
  <p>my php file.</p>
  
  <pre><code>&lt;?php
  //request.php
  $decoded = json_decode($_GET['json']);
  
  $json = array(
      'errorsNum'    =&gt;    2,
  
      'error'       =&gt;    array(
                          "error 1","error 2!"
                   )
  );
  
  $encoded = json_encode($json);
  
  die($encoded);
  
  ?&gt;
  </code></pre>
  
  <p>and finally my html file where I invoke ajax.</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;script src="request.js"&gt;
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;a href="Javascript:runAjax('vinoth')"&gt;call&lt;/a&gt;&lt;br&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Thanks in advance.</p>