【问题标题】:How to loop over JSON response with javascript如何使用 javascript 循环 JSON 响应
【发布时间】:2014-04-17 14:37:24
【问题描述】:

我正在使用以下 ajax:

       $.ajax({ 
                 type: 'POST', 
                 url: '/search/search.php', 
                 crossDomain: true,
                 data:  {data: data},
                 dataType: 'json', 
                 async: false,

                 success: function (response){ 
                    if (response.success) 
                        {
                           $('#search-results').show();

                           for(field in response.data){

                           error = response.field_errors[field];           

                               var name = field.name;
                               var barcode = field.barcode;
                               var serial = field.serial;

                               $("#searchname").html(name);
                               $("#searchbarcode").html(barcode);
                               $("#searchserial").html(serial);


                           }
                        } 
                    else {
                        console.log("fail");
                    }
                 },

               }); 

我正在尝试遍历从 php 返回的行,并将每一行作为一行放在我的 html 表中。我从 php 得到正确的响应,但我的循环没有显示任何内容的HTML。

HTML 表格

        <table class="table" id="search-results" style="display:none;">
          <thead>
            <tr>
              <th>#</th>
              <th>Name</th>
              <th>Serial</th>
              <th>Barcode</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>1</td>
              <td id="searchname"></td>
              <td id="searchbarcode"></td>
              <td id="searchserial"></td>
            </tr>
          </tbody>
        </table>

PHP

    $json = array();
    if($num > 0)
    {

        $json['success'] = TRUE;
        $i = 0;
 while ($row = mysql_fetch_array($sql))
 {
    $json[$i]['data']['name'] = $row['name'];
    $json[$i]['data']['barcode'] = $row['barcode'];
    $json[$i]['data']['serial'] = $row['serial'];

    $i++;
}
    }
    else
    {
        $json['success'] = FALSE;
    }

    echo json_encode($json);

【问题讨论】:

  • 怎么了?你期望它做什么?它做了什么?请提出一个真正的问题。
  • 我正在尝试为 php 响应中的每一行在我的 html 表中添加一行。它目前没有在 html 上显示任何内容,但给了我来自 php 的正确 json 响应
  • 为什么要使用循环?由于 ID 是唯一的,因此您只会用每一行覆盖您的值。 console.log(field); 在循环中为您提供了什么?
  • 我不想覆盖我的值,我已经用我的表格代码更新了我的问题。有没有办法为每一行生成html?
  • 另外var name = field.name; in for..in 没有意义。你可能需要var name = response.data[field].name;barcodeserial 也是如此。

标签: javascript php ajax loops


【解决方案1】:

您可以使用 jquery .each()。迭代数组,并使用 jquery .append() 添加表行:

如果数据是对象数组:

$.each(response.data, function( index, item ) {
    $('#search-results').append('<tr><td>' + item.name + '</td><td>' + item.barcode + '</td><td>' + item.serial + '</td><td></tr>');
});

如果是数组数组:

$.each(response.data, function( index, item ) {
    $('#search-results').append('<tr><td>' + item['name'] + '</td><td>' + item['barcode'] + '</td><td>' + item['serial'] + '</td><td></tr>');
});

https://api.jquery.com/jQuery.each/

编辑你的 php 创建奇怪的 json,因此你的问题。修复它:

$json = array();
if($num > 0)
{

    $json['success'] = TRUE;

    while ($row = mysql_fetch_array($sql))
    {
          $json['data'][]=array('name'=>$row['name'], 'barcode'=>$row['barcode'], 'serial'=>$row['serial']);
    }
}
else
{
    $json['success'] = FALSE;
}

echo json_encode($json);

【讨论】:

  • 我收到TypeError: object is undefined length = object.length
  • @user1738017 请编辑您的问题以向我显示 response.data 的内容,例如使用 console.log(response.data);或者只是在第一个位置显示创建它的 php 代码。
  • 完整回复是{"success":true,"0":{"data":{"name":"carla","barcode":null,"serial":"test"}},"1":{"data":{"name":"test","barcode":null,"serial":"test"}}}
  • @user1738017 你的 json 搞砸了。我已经编辑了我的答案来为你修复它。
  • 我已经更改了它,但现在我看到了这个错误`
    注意:使用未定义的常量序列 - 在 /Applications/ 中假设为“序列” XAMPP/xamppfiles/htdocs/search/search.php22

    注意:使用未定义的常量序列 - 假设'第 22/Applications/XAMPP/xamppfiles/htdocs/search/search.php 中的序列号
    {"success":true,"data":[ {"name":"carla","barcode":null,"serial":"test"},{"name":"test","barcode":null,"serial":"test"}]}`
猜你喜欢
  • 1970-01-01
  • 2015-08-05
  • 2016-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-01
  • 2016-07-04
  • 2018-11-27
相关资源
最近更新 更多