How to correctly give an ajax response to populate this jQuery variable?
<p>I am trying to get the below jQuery script working with AJAX. It currently populates my search box (<code>&lt;input type="text" id="search-names"&gt;</code>) correctly when I manually define <code>UserArray</code>:</p>

<pre><code> $(function() {
      var UserArray = [
            {"label": "Member#1 of User1", "value": "user1"},
            {"label": "Member#1 of User2", "value": "username2"},
            {"label": "Member#2 of User2", "value": "username2"},
            {"label": "Member#2 of User1", "value": "user1"},
            {"label": "Member#3 of User1", "value": "user1"}
      ];

      $("#search-names").autocomplete({

            //source: "/member/search",
            source: UserArray,
            minLength: 2,
            select: function (event, matched) {
                 console.log(matched)
                 window.location = window.location + '?_switch_user=' + matched.item.value
            }
      });
 })
</code></pre>

<p>However, I've not used AJAX before so I'm not sure what I'm missing to let me pull the data from another route (<code>/member/search</code>). My JSON controller is:</p>

<pre><code>/** 
 * @Route("/member/search", name="membersearch")
 */
public function memberSearch(Request $request)
{
    $response = new Response();
    $members = $this-&gt;getDoctrine()-&gt;getRepository('AppBundle:Members')-&gt;membersAndUsernames() ;

    foreach($members as $member) {
        $data[]=array($member-&gt;getFirstname(). ' '.$member-&gt;getSurname() . ' '.$member-&gt;getMemberId()-&gt;getUsername() );;
    }

    $response-&gt;setContent(json_encode($data));
    $response-&gt;headers-&gt;set('Content-Type', 'application/json');
    $response-&gt;send();
}
</code></pre>

<p>....this seems to return the data back to me as:</p>

<pre><code>[["Member#1 of User1", "user1"],["Member#1 of User2", "username2"]],
</code></pre>

<p>...instead of:</p>

<pre><code>[{"label": "Member#1 of User1", "value": "user1"},{"label": "Member#1 of User2", "value": "username2"}]
</code></pre>