title: Unexpected token in JSON at position 0 at JSON.parse
body: <p><strong>[SOLUTION]</strong> Saving the relevant .php files in ANSI solved this issue. Check the accepted answer for more information.</p>
  
  <p>I get this error in console when trying to parse JSON coming from PHP's <code>json_encode()</code>:</p>
  
  <pre><code>VM2538:1 Uncaught SyntaxError: Unexpected token   in JSON at position 0
  at JSON.parse (&lt;anonymous&gt;)
  at Object.success (products.js:169)
  at i (jquery-v3.2.1.min.js:2)
  at Object.fireWith [as resolveWith] (jquery-v3.2.1.min.js:2)
  at A (jquery-v3.2.1.min.js:4)
  at XMLHttpRequest.&lt;anonymous&gt; (jquery-v3.2.1.min.js:4)
  </code></pre>
  
  <p>I tried setting MySql's server and tables collation to utf8_unicode_ci as suggested in some answers but that made no difference.
  Also added <code>&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;</code> to HTML head and <code>header('Content-Type: application/json; charset=utf-8');</code> to PHP file, which changed the output of <code>json_encode()</code> a bit. (Both examples included at the bottom)</p>
  
  <p><strong>[UPDATED]</strong> Here's the JavaScript:
  (atm I just wanna log a value to the console, so success function is not complete)</p>
  
  <pre><code>function loadProducts() {
  $.ajax({
    method: 'GET',
    dataType: 'json',
    url: 'ajax/list_products.php',
    success: function(products) {
      var json = products;
      console.log(json);
      console.log(json[0].Code);
      console.log(json[0]["Code"]);
    }
  });
  </code></pre>
  
  <p>}</p>
  
  <p>This is the PHP code from <code>list_products.php</code>:</p>
  
  <pre><code>&lt;?php
  $root = $_SERVER['DOCUMENT_ROOT'];
  require_once $root . "/commander/class/product.php";
  
  $product = new Product();
  $productList = $product-&gt;listProducts();
  
  if (!empty($productList) &gt; 0) {
    echo json_encode($productList);
  }else {
    echo "Error - Records are empty.";
  }
  </code></pre>
  
  <p>And this is the output from <code>list_products.php</code> without <code>header('Content-Type: application/json; charset=utf-8');</code>: <a href="https://pastebin.com/0AeqTtmd" rel="nofollow noreferrer">PasteBin</a></p>
  
  <p>With <code>header('Content-Type: application/json; charset=utf-8');</code>:
  <a href="https://pastebin.com/SEa4m6GK" rel="nofollow noreferrer">PasteBin</a></p>