title: Improperly formatted JSON being passed through ajax
body: <p>hey guys having really frustrating error here.  I have a JSON formatted array I'm passing from a PHP script to javascript through AJAX, and although I've run the outputted JSON through a validator the javascript continues to fail at the <code>jQuery.parseJSON()</code> step giving me an error.<br>
  The error is <code>Uncaught Syntax Error unexpected token &lt;</code></p>
  
  <p>Anyone have any idea what the problem could be?</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;script src="http://code.jquery.com/jquery-1.10.1.min.js"&gt;&lt;/script&gt;
  &lt;script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  function showHint(str)
  {
    var xmlhttp;
    if (str.length==0)
    { 
      document.getElementById("txtHint").innerHTML="";
      return;
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200)
      {
        var data=xmlhttp.responseText;
        document.getElementById("txtHint").innerHTML=data;
        var jsonData = jQuery.parseJSON('{"name":"John"}');
        alert("Got here Second!");
        document.getElementById("hints").innerHTML = jsonData[0][1];
        alert("Probably failed...");
      }
    }
    xmlhttp.open("GET","getSuggestion.php?q="+str,true);
    xmlhttp.send();
  }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;h3&gt;Start typing a name in the input field below:&lt;/h3&gt;
  &lt;form action=""&gt; 
  First name: &lt;input type="text" id="txt1" onkeyup="showHint(this.value)" /&gt;
  &lt;/form&gt;
  &lt;p&gt;Suggestions: &lt;span id="txtHint"&gt;&lt;/span&gt;&lt;/p&gt; 
  &lt;p&gt;Actual Hints: &lt;span id="hints"&gt;&lt;/span&gt;&lt;/p&gt; 
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>PHP:</p>
  
  <pre><code>&lt;?php
  header('Content-type: application/json');
  function getHint($companyName)
  {
      try
      {
          $first = getJson($companyName);
          $temp = strpos($first, "(") + 1;
          $second = substr($first, $temp, strlen($first) - $temp - 1);//this line produces valid JSON
          $json = json_decode($second,true);
      }
      catch(Exception $e)
      {
          echo "caught Exception " , $e-&gt;getMessage();
      }
      return $json;
  }
  function getJson($companyName)
  {
      $partial = urlencode($companyName);
      $searchURL = "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=" . $partial . "&amp;callback=YAHOO.Finance.SymbolSuggest.ssCallback";
      $filesize = 4000;
      $handle = fopen($searchURL,"r");
      $jsonData = fread($handle,$filesize);
      fclose($handle);
      return $jsonData;
  }
  $q=$_GET["q"];
  $roof = getHint($q);
  //TODO: kill if no suggestion
  $collection = $roof[ResultSet][Result];
  echo json_encode($collection);
  ?&gt;
  </code></pre>
  
  <p>Sample JSON</p>
  
  <pre><code>[{"symbol":"A","name":"Agilent Technologies Inc.","exch":"NYQ","type":"S","exchDisp":"NYSE","typeDisp":"Equity"},{"symbol":"^DJI","name":"Dow Jones Industrial Average","exch":"DJI","type":"I","typeDisp":"Index"},{"symbol":"AAPL","name":"Apple Inc.","exch":"NMS","type":"S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"BAC","name":"Bank of America Corporation","exch":"NYQ","type":"S","exchDisp":"NYSE","typeDisp":"Equity"},{"symbol":"FNMA","name":"Federal National Mortgage Association","exch":"OBB","type":"S","exchDisp":"OTC BB","typeDisp":"Equity"},{"symbol":"T","name":"AT&amp;T, Inc.","exch":"NYQ","type":"S","exchDisp":"NYSE","typeDisp":"Equity"},{"symbol":"AFFY","name":"Affymax, Inc.","exch":"NMS","type":"S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"AMZN","name":"Amazon.com Inc.","exch":"NMS","type":"S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"DIA","name":"SPDR Dow Jones Industrial Average","exch":"PCX","type":"E","typeDisp":"ETF"},{"symbol":"AUDUSD=X","name":"AUD\/USD","exch":"CCY","type":"C","typeDisp":"Currency"}] 
  </code></pre>
  
  <p>Thanks for any help!</p>