title: Convert PHP JSON encoded array to JavaScript array in AJAX?
body: <p>I am unable to convert the AJAX response into a JavaScript array. All I receive is <code>[object Object]</code> in my alert box.</p>
  
  <pre><code>var url = 'list_devices.php';
  var modurl = url;
  alert(modurl);
  ajax.open("GET", modurl, true);
  ajax.onreadystatechange = function () {
      if (ajax.readyState == 4) {
          if (ajax.status == 200) {
              //var json = JSON.stringify(ajax.responseText);
              var me = JSON.parse(ajax.response);
              //var me=jQuery.parseJSON(responseData);
              //alert(me);
              alert(json);
              //var uid=new Array();
              //uid=me.split(",");
              //$.mobile.navigate("#page1");
              //$('#devices').empty();
              //var temp=document.createElement('li');
              //for (var i=0; i&lt;uid.length; i++) {
              //$('#devices').append('&lt;li &gt;&lt;p&gt;'+uid[i]+'&lt;/p&gt;&lt;/li&gt;'); --&gt;
              //}
              //$('#devices').append(temp);
          }
      }
  }
  ajax.send(null);
  }
  </code></pre>
  
  <p><strong>PHP:</strong></p>
  
  <p>(I am trying to fetch all the IMEI of the <code>user_id</code> stored in a session.)</p>
  
  <pre><code>&lt;?php
    header("content-type:text/javascript");
    session_start();
    $sql=new mysqli("hostname","user","pass","dbname");
    if(!$sql):
        echo "error connecting to database";
        die();
    else:
       $i=0;
       $temp=[];
       $q="SELECT imei FROM tbl_user_device where user_id='".$_SESSION['user_id']."'";
       $result=$sql-&gt;query($q);
       while($output=$result-&gt;fetch_array()):
          echo json_encode($output);      
       endwhile;
    endif;
  ?&gt;
  </code></pre>