如何将xmlhttp.responseText转换为js数组并在js中打印

时间:2014-01-24 10:59:16

标签: javascript php

如何将xmlhttp.responseText转换为js数组

这是我的{"name":"Eswara Manikanta Varma","email":"eswar1251@gmail.com","mobile":"9966578911"}来自xmlhttp.responseText,所以现在我要转换为js数组.. 当我警告var对象时,警报看起来像这样[object Object]  我想将其打印为jsarray [mobile]

我的第1页:

<!doctype html>
<html>
       <head>
               <meta charset="utf-8">
               <title>Invoice</title>
               <link rel="stylesheet" href="style.css">

               <script src="script.js"></script>
       <script>
function showUser(str)
{
if (str=="")
 {
 document.getElementById("txtHint").innerHTML="";
 return;
 }
if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
 }
else
 {// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
xmlhttp.onreadystatechange=function()
 {
 if (xmlhttp.readyState==4)
   {
        var object = JSON.parse(xmlhttp.responseText);
        alert(object); 
   document.getElementById("txtHint").innerHTML=xmlhttp.responseText;

   }
 }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
       <body>
               <header>
                       <h1>Invoice</h1>
                       <address contenteditable>
                               <p>Jonathan Neal</p>
                               <p>101 E. Chapman Ave<br>Orange, CA 92866</p>
                               <p>(800) 555-1234</p>
                       </address>
                       <span><img alt="" src="logo.png"><input type="file" accept="image/*"></span>
               </header>
               <article>
                       <h1>Recipient</h1>
                       <address contenteditable>
                               <p>Some Company<br>c/o Some Guy</p>
                       </address>
           <form>
                       <table class="meta">
                               <tr>
                                       <th><span contenteditable>Invoice #</span></th>
                                       <td><span contenteditable>101138</span></td>
                               </tr>
                               <tr>
                                       <th><span contenteditable>Date</span></th>
                                       <td><span contenteditable>January 1, 2012</span></td>
                               </tr>
                               <tr>
                                       <th><span contenteditable>Amount Due</span></th>
                                       <td><span id="prefix" contenteditable>$</span><span>600.00</span></td>
                               </tr>
                       </table>
                       <table class="inventory">
                               <thead>
                                       <tr>
                                               <th><span contenteditable>Item</span></th>
                                               <th><span contenteditable>Description</span></th>
                                               <th><span contenteditable>Rate</span></th>
                                               <th><span contenteditable>Quantity</span></th>
                                               <th><span contenteditable>Price</span></th>
                                       </tr>
                               </thead>
</body>
</html>

第2页:

<?php
$q = $_GET['q'];
$con = mysqli_connect('localhost','root','enter','esmart');
if (!$con)
 {
 die('Could not connect: ' . mysqli_error($con));
 }

mysqli_select_db($con,'esmart');
$sql="SELECT * FROM suppliers WHERE name LIKE '%$q%'";

$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
 {
    $x['name']=$row['name'];
    $x['email']=$row['email'];
    $x['mobile']=$row['mobile'];
 }
echo json_encode($x);

mysqli_close($con);
?> 

2 个答案:

答案 0 :(得分:1)

我认为您的问题出在PHP代码中。 有了这个:

while($row = mysqli_fetch_array($result))
 {
    $x['name']=$row['name'];
    $x['email']=$row['email'];
    $x['mobile']=$row['mobile'];
 }
echo json_encode($x);

您将只获得查询的最后一行。 为了能够得到一个数组,你可能需要这样的东西:

$response = array();
while($row = mysqli_fetch_array($result))
 {
    $x['name']=$row['name'];
    $x['email']=$row['email'];
    $x['mobile']=$row['mobile'];
    $response[] = $x;
 }
echo json_encode($response);

现在你将获得[{"name":"Eswara Manikanta Varma","email":"eswar1251@gmail.com","mobile":"9966578911"}, ...],在JSON.parse之后它将是一个对象数组。

答案 1 :(得分:0)

尝试使用:

  

mysqli_fetch_assoc($结果)

而不是

  

mysqli_fetch_array($结果)