title: SyntaxError: JSON.parse: unexpected character error in AJAX call
body: <p>I'm an engineering intern at a software company, and my current project is to create an HTML page that uses AJAX and PHP to get data from a database. I've never posted on this site before, I've always looked for similar posts and used the info I find. I've been looking for a solution to my problem for the last 2 hours or so. My AJAX call returns the error: "SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data."</p>
  
  <p>I've tried lots of things, namely:</p>
  
  <ol>
  <li>Adding dataType: 'json' to the options in $.ajax.</li>
  <li>Adding header("Content-type: application/json"); to the PHP code.</li>
  <li>Using console.log($.parseJSON(data)) in the success function.</li>
  <li>Reformatting my code in PHP to include quotes or not include them in the array</li>
  </ol>
  
  <p>It seems like the problem is in the formatting of the data my page receives from my PHP script, which is as follows:</p>
  
  <pre><code>header('Content-type: application/json');
  $testarray = array("Name" =&gt; 'America', "Code" =&gt; '12345');
  echo json_encode($testarray);
  </code></pre>
  
  <p>In my jquery segment, I want to retrieve JSON data from my PHP script (which will eventually use a MySQL DB) and add the data as elements in my list box, CountriesList. My AJAX call is as follows:</p>
  
  <pre><code>$.ajax({
       cache: false,
       url: 'results.php',
       method: 'POST',
       dataType: 'json',
       data: "",
       success: function (data, textStatus, jqXHR){
             //I get an error with the following line
             var jsonData = JSON.parse(data);
             $(jsonData).each(function (i, element)
             {
                   var htmlSelect = document.getElementById("CountriesList");
                   var opt = document.createElement("option");
                   opt.text = element.Name;
                   opt.value = element.Code;
                   htmlSelect.options.add(opt);
              });
              },
              error: function(jsXHR, textStatus, errorThrown)
              {
                          alert('Error: ' + errorThrown)
              }
               });
  </code></pre>
  
  <p>It's possible that there are other errors in this code since I haven't been able to debug past this formatting issue. When I open the PHP script by itself, I get this as a response: {"Name":"America","Code":"12345"}. Which, as far as I'm aware, is correctly formatted JSON.</p>
  
  <p>I'm at a loss. Any help is appreciated.</p>