2014-01-24 64 views
0

如何xmlhttp.responseText轉換爲JS陣列如何xmlhttp.responseText轉換爲JS陣列和打印在JS

這是我從{"name":"Eswara Manikanta Varma","email":"[email protected]","mobile":"9966578911"}獲得xmlhttp.responseText所以現在我想在JS數組轉換.. 當過我我提醒VAR對象的警報似乎這樣的翻譯: 我想打印出來作爲jsarray [移動]

我第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); 
?> 

回答

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":"[email protected]","mobile":"9966578911"}, ...]JSON.parse後,這將是對象的數組。

0

嘗試使用:

mysqli_fetch_assoc($結果)

,而不是

mysqli_fetch_array($結果)