我正在使用 $.ajax 和 asp.net 中的 json 从数据库中获取记录。
一切正常,但是当记录数大于 1000 时,它会崩溃并显示“500(内部服务器错误)”错误消息。
jQuery 代码
$.ajax({ type: "post",
url: "Default.aspx/SELECT_ALL",
data: _data,
contentType: "application/json;charset=utf-8", dataType: "json",
success: function (data) {
if (data.d != null || data.d != 'null') {
var Items = data.d;
if (Items.length > 0) {
// Create Table header and prepare the body
$.fn.create_table();
for (var r = 0; r < Items.length; r++) {
// Add new row and columns
var tRow = tBody.insertRow(-1);
for (var x = 0; x < Cols.length; x++) {
var td = tRow.insertCell(-1);
var input = document.createElement('input');
input.type = 'text';
input.id = columns[x] + '-' + row;
if (x == 1 || x == 2 || x == 3) {
input.setAttribute("class", "validate text");
} else if (x == 4) {
input.setAttribute("class", "validate number number-only");
}
if (x == 0) {
input.type = 'checkbox';
input.value = row;
} else {
switch (x) {
case 1:
//input.value = Items[r].BMLCode;
td.innerHTML = '<input type="text" id="' + columns[x] + '-' + row + '" value="' + Items[r].Code + '" /><input type="hidden" name="ID" id="ID" value="' + Items[r].ID + '" /><input type="hidden" name="updated" id="updated" />';
break;
case 2:
input.value = Items[r].Description;
break;
case 3:
input.value = Items[r].Unit;
break;
case 4:
input.value = Items[r].Price;
input.setAttribute("onblur", '$.fn.adRow(' + r + ');');
break;
default:
input.value = 'Error';
break;
}
}
if (x == 0) {
td.innerHTML = '<input type="checkbox" id="' + columns[x] + '-' + row + '" value="' + row + '" /><label>' + row + '</label>';
} else if ($.inArray(x, [1, 3, 4, 5])) {
td.appendChild(input);
}
if (x >= columns.length - 1) {
row++;
$(".number-only").each(function () {
$(this).numberBox();
});
}
}
}
} else {
$.fn.create_table();
}
}
}
});
ASP.NET 代码
#region Items
public class clsCodes
{
public string ID { set; get; }
public string Code { set; get; }
public string Description { set; get; }
public string Unit { set; get; }
public string Rate { set; get; }
}
#endregion
#region Select All
[WebMethod]
public static clsCodes[] SELECT_ALL(string Activity_ID)
{
try
{
List<clsCodes> _Return = new List<clsCodes>();
Database db = new Database();
DataTable table = null;
Dictionary<string, object> Parameters = new Dictionary<string, object>();
Parameters.Add("Procedure", "usp_codes");
Parameters.Add("@Activity_ID", MajorActivity_ID);
db.Open();
table = db.ExecuteTable(Parameters);
db.Close();
if (table != null && table.Rows.Count > 0)
{
int iRow = 0;
foreach (DataRow dRow in table.Rows)
{
if (iRow >= 1000)
{
break;
}
clsCodes _Code = new clsCodes();
_Code.ID = dRow["ID"].ToString();
_Code.Code = dRow["Code"].ToString();
_Code.Description = dRow["Description"].ToString();
_Code.Unit = dRow["Unit"].ToString();
_Code.Rate = dRow["Rate"].ToString();
_Return.Add(_Code);
iRow++;
}
}
return _Return.ToArray();
}
catch (Exception ex)
{
return null;
}
}
#endregion
注意:我在 javascript 中使用 HTML DOM 表对象来创建表。