du1913 2014-01-24 10:59
浏览 302

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

How to convert xmlhttp.responseText to js array

this is my {"name":"Eswara Manikanta Varma","email":"eswar1251@gmail.com","mobile":"9966578911"} getting from xmlhttp.responseText so now i want to convert in js array.. when ever i'm alerting var object the alert seem like this [object Object] I would like to print it as jsarray[mobile]

my page 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>

page 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条回答 默认 最新

  • dragon071111 2014-01-24 11:51
    关注

    I think your problem is in the PHP code. with this:

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

    You're going to get only the last row of your query. To be able to get an array you may need something like this:

    $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);
    

    Now you'll get [{"name":"Eswara Manikanta Varma","email":"eswar1251@gmail.com","mobile":"9966578911"}, ...] and after JSON.parse it will be an array of objects.

    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测