2016-05-31 76 views
0

我试图记录追加到我这正从返回JSON AJAX请求加载表,但如果我尝试使用output.length它返回一个大数目而不是750条记录。这将导致for环路为10000次,而不是750为什么会这样运行?JSON对象的长度,而不是返回大值的预期750个记录

$(document).ready(function() { 
    getData(); 
}); 

function getData() { 
    $.ajax({ 
     data: { 
      action: 'getData' 
     }, 
     url: "api/ajaxcall.php", 
     type: 'post', 
     async: false, 
     dataType: 'html', 
     success: function(output) { 
      console.log(output.length); 
      // buildTable(result); 
     } 
    }); 
} 

function buildTable(output) { 
    for (var i = 0; i < output.length; i++) { 
     $('<tr>').append(
      $('<td>').text(output[i].naam), 
      $('<td>').text(output[i].perc), 
      $($('<input id="' + output[i].id + '" onclick="editData(this)" type = "button" value = "Edit" />')), 
      $($('<input id="' + output[i].id + '" onclick="readData(this)" type = "button" value = "Read" />')), 
      $($('<input id="' + output[i].id + '" onclick="deleteRow(' + output[i].id + ')" type = "button" value = "Delete" />')) 
     ).appendTo('#tableA'); 
    } 
} 
+3

你'dataType'不正确。长度可能是JSON _string_的长度。 – robertklep

+2

*不要*使用'异步:FALSE'。这是非常糟糕的做法。你也应该看看在使用委派的事件处理程序在'上*'属性。 –

回答

0

检查与此大概

function getData() { 
$.ajax({ 
    data: { 
     action: 'getData' 
    }, 
    url: "api/ajaxcall.php", 
    type: 'post', 
    async: false, 
    dataType: 'html', 
    success: function(output) { 
     output = JSON.parse(output.d); //May it need to parse the string 
     console.log(output.length); 
     // buildTable(result); 
    } 
}); 

}

+2

或更改“数据类型”为“JSON” –

相关问题