How do I make use of JSON response from JQuery AJAX request?
<p>I am using PHP and MySQL to swap out some content.
A click handler sends an AJAX request to the PHP script which performs a query, then encodes and prints the result as a JSON object.</p>

<p>All that seems to work, but I seem to be stuck on the most silly thing:
I can't work out how to actually use the result, as in set the individual values to JavaScript variables.</p>

<p>Edit: I added a couple more things I tried, to get a specific value to print out to the console, no luck thus far.</p>

<p>Edit: Found the correct syntax:</p>

<pre><code>response[0].foo
</code></pre>

<p>javascript:</p>

<pre><code>var listId = $(this).children('.hidden-id').html();

        var options = new Object();
        options.data = listId;
        options.dataType = 'json';
        options.type = 'POST';

        options.success = function(response) {
            console.log(response[0].foo);
        };

        options.url = './inc/change_list.php';

        $.ajax(options);
</code></pre>

<p>the PHP script:</p>

<pre><code>$list_id = $_POST['list_id'];
    $q = "SELECT id, title, description, position FROM items WHERE list_id=$list_id ORDER BY position ASC";
    $r = mysqli_query($dbc, $q);
    if (mysqli_num_rows($r) &gt; 0) {

        $result = array();
        while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
            $add_result = [
                'id' =&gt; $row['id'],
                'title' =&gt; $row['title'],
                'description' =&gt; $row['description']
            ];
            $result[] = $add_result;
        }
        $result = json_encode($result);
        echo $result;

    } else {
        echo '{"result":"failure"}';
    }
</code></pre>