首页 > 解决方案 > 如何在ajax调用中附加来自rest服务的json数据

问题描述

当我试图在 ajax 调用中附加来自休息服务的数据时。只有最后一个值被附加到表中。

   function getReporteeList() {
$.ajax({
url:'http://localhost:8088/JirasTrackingApp/reporter/Reportees/ReporteeList/'+ $("#ManagerId").val(),
type:'GET',
dataType: 'json',
success: function(result){      

           var content = '';
           $.each(result,function(key,value){
               content += '<tr>';
               content = '<td>'+value.Name+'</td>';
               content = '<td>'+value.UserId+'</td>';
               content += '<td>'+value.count+'</td>';
               content += '</tr>';

           });
           $('#employee_table').append(content);


         }
   });
 } 

以这种格式返回的 json 数据:

   0: {UserId: "at1234", count: 0, Name: "Amreen Taj"}
   1: {UserId: "AR1234", count: 0, Name: "Anagha R"}
   2: {UserId: "MS1234", count: 4, Name: "Madhusudan S"}

但我得到的输出是:

    Name    UserName    Count   
   MS1234    4

这是我的 html 表:

       <table id = "employee_table">
            <tr>
                <th>Name</th>
                <th>UserId</th>
                <th>Count</th>
            </tr>
        </table>

请让我知道我哪里出错了

标签: javascript

解决方案


排队

          content += '<tr>';
               content = '<td>'+value.Name+'</td>';
               content = '<td>'+value.UserId+'</td>';
               content += '<td>'+value.count+'</td>';
               content += '</tr>';

你有时有+=,有时有=;

它应该会爆炸,但浏览器会自动修复无效的 HTML 结构。


推荐阅读