-6

如果我的 ajax 调用返回了一些数据。我需要循环并创建一个 Html 字符串,该字符串创建如下表:

 ID    NAME                  Object Type
 ==============================================
 1     SWT-F1-S32-RTR-1      Network Switch
 2     SWT-44-SR1-RTR-1      Network Switch
 etc.

这是我的 json 数据的样子:

[
   {
      "name": "SWT-F1-S32-RTR-1",
      "id": "1",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-44-SR1-RTR-1",
      "id": "2",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-44-SR1-SW-1",
      "id": "3",
      "dict_value": "Network chassis"
   },
   {
      "name": "SWT-F1-S32-SW-2",
      "id": "4",
      "dict_value": "Network chassis"
   },
   {
      "name": "SWT-44-165-SW-1",
      "id": "5",
      "dict_value": "Network chassis"
   },
   {
      "name": "SWT-44-263-SW-1",
      "id": "6",
      "dict_value": "Network chassis"
   },
   {
      "name": "SWT-11-228-SW-1",
      "id": "7",
      "dict_value": "Network chassis"
   },
   {
      "name": "SWT-F1-S32-SW-1",
      "id": "8",
      "dict_value": "Network chassis"
   },
   {
      "name": "SWT-F2-204-SW-1",
      "id": "9",
      "dict_value": "Network chassis"
   },
   {
      "name": "SWT-R1-CC104-SW-1",
      "id": "10",
      "dict_value": "Network chassis"
   },
   {
      "name": "SWT-5-103-SW-1",
      "id": "11",
      "dict_value": "Network chassis"
   },
   {
      "name": "SWT-F1-1PF-SW-1",
      "id": "12",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-F1-1PE-SW-1",
      "id": "13",
      "dict_value": "Network chassis"
   },
   {
      "name": "SWT-11-123-SW-1",
      "id": "15",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-32-M402-SW-1",
      "id": "17",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-66-102-SW-1",
      "id": "18",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-R1-CC011-SW-1",
      "id": "19",
      "dict_value": "Network chassis"
   },
   {
      "name": "SWT-R1-CC239-SW-1",
      "id": "20",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-R1-CCPH-SW-1",
      "id": "21",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-32-K218-SW-1",
      "id": "23",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-SB1-204-SW-1",
      "id": "24",
      "dict_value": "Network chassis"
   },
   {
      "name": "SWT-SB3-107-SW-1",
      "id": "25",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-SB4-110-SW-1",
      "id": "26",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-5-119-SW-1",
      "id": "27",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-F2-113-SW-1",
      "id": "39",
      "dict_value": "Network chassis"
   },
   {
      "name": "Core Switch HSRP",
      "id": "91",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-MLO-106-SW-1",
      "id": "275",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-STP-001-SW-1",
      "id": "277",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-F1-169-SW-1",
      "id": "281",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-R1-F108-SW-1",
      "id": "283",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-GH-001-SW-1",
      "id": "285",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-11-228-SW-2",
      "id": "301",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-44-010-SW-1",
      "id": "303",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-44-141-SW-1",
      "id": "305",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-F1-169-SW-2",
      "id": "309",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-F2-114-SW-1",
      "id": "311",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-66-114-SW-1",
      "id": "313",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-R1-1109-SW-1",
      "id": "315",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-32-L009-SW-1",
      "id": "317",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-SB1-204-SW-2",
      "id": "319",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-5-103-SW-2",
      "id": "321",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-5-114-SW-1",
      "id": "323",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-F1-1PL-SW-1",
      "id": "325",
      "dict_value": "Network chassis"
   },
   {
      "name": "CAES-0xx-CAGE",
      "id": "365",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-R1-H109-SW-1",
      "id": "379",
      "dict_value": "Network chassis"
   },
   {
      "name": "SWT-FM-001-SW-1",
      "id": "385",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-LSQ-BSMT-SW-1",
      "id": "415",
      "dict_value": "Network switch"
   },
   {
      "name": "SWT-44-152-SW-1",
      "id": "417",
      "dict_value": "Network chassis"
   }
]

编辑 1 这是我到目前为止的代码:

    $.ajax({
            url:"<?php echo site_url('switches/getallswitches/');?>",
            type:'POST',
            dataType:'json',
            success: function(returnDataFromController) {
                    var htmlstring;
                    htmlstring="<select name='L2Locations' id='L2Locations'>";
                    htmlstring = htmlstring + "<option value='all'>All</option>";

                    //console.log(returnDataFromController);
                     var JSONdata=returnDataFromController;
                     console.log(JSONdata);
                     if (JSONdata.length != 0) 
                     {
                            $.each(JSONdata, function(i, objswitch) {
                            var row = $('<tr class="alt2 section_1">').appendTo($('#table'));
                            row.append($('<td>').append($('<strong>').text(objswitch.name)));
                            $.each(objswitch.id, function(j, comparisonValue) {
                                row.append($('<td>').text(comparisonValue));
                            });
                        });

                     }

                }//success


        });//end ajax

它基于另一篇文章中的示例。但我不确定如何将“行”转换为 html 字符串,因此我最终可以执行 (#nameofdiv).html(htmlstring) 之类的操作。

对不起,到目前为止所有读过这篇文章的人。这是疯狂的一天,并不是说你必须在发表讨厌的言论之前查看我的其他帖子,但为了它的价值,我今天一直在与其他人在 stackoverflow 上解决一些有趣的 ajaax 问题。而且我想我太快了,无法进入。我立马中招了!哇。无论如何。谢谢你的耐心。希望这篇文章更清楚一点。

4

1 回答 1

2

问题是循环中引用的表名不正确。

于 2012-09-19T20:44:10.033 回答