Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript循环JSON响应_Javascript_Php_Ajax_Loops - Fatal编程技术网

如何使用javascript循环JSON响应

如何使用javascript循环JSON响应,javascript,php,ajax,loops,Javascript,Php,Ajax,Loops,我正在使用以下ajax: $.ajax({ type: 'POST', url: '/search/search.php', crossDomain: true, data: {data: data}, dataType: 'json', async: false,

我正在使用以下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>
您可以使用jquery.each()。要对数组进行初始化,请使用jquerys.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>');
});
您可以使用jquery.each()。要对数组进行初始化,请使用jquerys.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>');
});
您可以使用jquery.each()。要对数组进行初始化,请使用jquerys.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>');
});
您可以使用jquery.each()。要对数组进行初始化,请使用jquerys.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>');
});



怎么了?你期望它做什么?它的作用是什么?请问一个真实的问题。我正在尝试为php响应中的每一行向html表中添加一行。它目前在html上没有显示任何内容,但从PHPW中给了我正确的json响应。您为什么使用循环?由于ID是唯一的,因此您将仅用每一行覆盖您的值。以及
console.log(字段)的作用在循环中给你?我不想覆盖我的值,我已经用表中的代码更新了我的问题。有没有办法为每一行生成html?还有
var name=field.namein
for..in
没有意义。您可能需要
var name=response.data[field].name。而
条码
串行
也一样。怎么了?你期望它做什么?它的作用是什么?请问一个真实的问题。我正在尝试为php响应中的每一行向html表中添加一行。它目前在html上没有显示任何内容,但从PHPW中给了我正确的json响应。您为什么使用循环?由于ID是唯一的,因此您将仅用每一行覆盖您的值。以及
console.log(字段)的作用在循环中给你?我不想覆盖我的值,我已经用表中的代码更新了我的问题。有没有办法为每一行生成html?还有
var name=field.namein
for..in
没有意义。您可能需要
var name=response.data[field].name。而
条码
串行
也一样。怎么了?你期望它做什么?它的作用是什么?请问一个真实的问题。我正在尝试为php响应中的每一行向html表中添加一行。它目前在html上没有显示任何内容,但从PHPW中给了我正确的json响应。您为什么使用循环?由于ID是唯一的,因此您将仅用每一行覆盖您的值。以及
console.log(字段)的作用在循环中给你?我不想覆盖我的值,我已经用表中的代码更新了我的问题。有没有办法为每一行生成html?还有
var name=field.namein
for..in
没有意义。您可能需要
var name=response.data[field].name。而
条码
串行
也一样。怎么了?你期望它做什么?它的作用是什么?请问一个真实的问题。我正在尝试为php响应中的每一行向html表中添加一行。它目前在html上没有显示任何内容,但从PHPW中给了我正确的json响应。您为什么使用循环?由于ID是唯一的,因此您将仅用每一行覆盖您的值。以及
console.log(字段)的作用在循环中给你?我不想覆盖我的值,我已经用表中的代码更新了我的问题。有没有办法为每一行生成html?还有
var name=field.namein
for..in
没有意义。您可能需要
var name=response.data[field].name。对于
barcode
serial
也一样。我得到
TypeError:object是未定义的长度=object.length
@user1738017请编辑您的问题以向我显示response.data的内容,例如使用console.log(response.data);完整的响应是
{“success”:true,“0”:{“data”:{“name”:“carla”,“barcode”:null,“serial”:“test”},“1”:{“data”:{“name”:“test”,“barcode”:null,“serial”:“test”}}
@user1738017您的json搞乱了。我已经编辑了我的答案来为您修复它。我已经更改了它,但是现在我看到了这个错误`
注意:在第22行的/Applications/XAMPP/xamppfiles/htdocs/search/search.php中使用了未定义的常量serial-假设为“serial”/Applications/XAMPP/xamppfiles/htdocs/search/search.php第22行
{“success”:true,“data”:[{“name”:“carla”,“barcode”:null,“serial”:“test”},{“name”:“test”,“barcode”:null,“serial”:“test”}}`我得到
类型错误:object是未定义的长度=object.length
@user1738017请编辑你的问题,向我显示响应的内容。data,例如,带有console.log(response.data);完整的响应是
{“success”:true,“0”:{“data”:{“name”:“carla”,“barcode”:null,“serial”:“test”},“1”:{“data”:{“name”:“test”,“barcode”:null,“serial”:“test”}}
@user1738017您的json搞乱了。我已经编辑了我的答案来为您修复它。我已经更改了它,但是现在我看到了这个错误`
注意:在第22行的/Applications/XAMPP/xamppfiles/htdocs/search/search.php中使用了未定义的常量serial-假设为“serial”/第22行的Applications/XAMPP/xamppfiles/htdocs/search/search.php
{“success”:true,“data”:[{“name”:“carla”,“barcode”:null,“serial”:“test”},{“name”:“test”,“barcode”:null,“serial”:“test”}}`I get
TypeError:对象未定义长度=obj