Note that there are some explanatory texts on larger screens.

plurals
  1. POError - Passing an array from PhP to jQuery using JSON
    text
    copied!<p>I apologise, since I know this has been asked <a href="https://stackoverflow.com/questions/5277100/pass-php-array-to-jquery">before</a>, more than <a href="https://stackoverflow.com/questions/15461417/how-do-you-assign-a-php-array-to-jquery-array">once</a>.</p> <p>I've been struggling with this little bugger for quite a few hours now, and I cannot seem to find my mistake.</p> <p>The problem is simple - I have a PhP array which I want to pass over, from the server side to the client side, using jQuery AJAX with the data in JSON format.</p> <p>The code for my Client-side Ajax call is</p> <pre><code>$.ajax({ url: 'php/test/pass_array.php', method: 'POST', data: {test1: 'abc', test2: 'cde'}, //Random input data dataType: 'json', success: function(response_data) { console.log(response_data); }, error: function(jqXHR, textStatus, errorThrown) { console.log(arguments); console.log('Error: ' + errorThrown + ' ' + textStatus + ' ' + jqXHR); } }); </code></pre> <p>And the code for my server-side pass_array.php script is</p> <pre><code>header('Content-type: application/json; charset=utf-8'); echo json_encode(array("Name"=&gt;"Johnny")); //Build JSON object from the array </code></pre> <p>As you can see, this is pretty basic, but it's not working. On the client side, the AJAX response is never received.</p> <p>When I check the browser console log, I get </p> <pre><code>Error: SyntaxError: Unexpected token parsererror [object Object] </code></pre> <p>What the <strong><em>devil</em></strong> am I doing wrong? </p> <p>All my files are in UTF-8 encoding. This also works if I remove the dataType: 'json' and the heading() on the PhP side. In that case, I get the following string:</p> <pre><code>{"Name":"Johnny"} </code></pre> <h2>UPDATE</h2> <p>Commenting out the header() code in the PhP side makes the console show the following error:</p> <pre><code>Uncaught SyntaxError: Unexpected token o </code></pre> <p>How can there by syntax errors? I create a PhP array and encode it using json_encode.</p> <h2>UPDATE #2</h2> <p>Fixed it.</p> <p>On my PhP file I had (only one more!) line</p> <pre><code>include '../core/db_io.php'; </code></pre> <p>I commented that line out and it worked fine.</p> <p>This file contains methods to query my databases. Since I did not call any methods or objects from this file, I left it uncommented - thinking it made no difference.</p> <p>It looks like including that db_io.php file added a Byte Order Mark character (\ufeff) to the JSON string.</p> <p>The complete string being sent to the Client side was</p> <pre><code>\ufeff{"Name":"Johnny"} </code></pre> <p>It seems this db_io.php file was encoded with BOM. Changing its encoding (using notepad++) fixed that.</p> <p>Amazing how a simple include instruction can mess up an AJAX call.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload