我想從數據庫加載值到HTML表使用JSON和C#.net。我沒有顯示超過1427的記錄,並顯示錯誤「意外的令牌<」。我在網上登記,但我無法得到結果。不顯示記錄超過1427
在此先感謝
我曾嘗試:
// JSON
$(document).ready(function() {
bindData();
});
function bindData() {
$.ajax({
type: "POST",
url: "MyTestForm.aspx/getData",
data: "{}",
contentType: "application/json;charset=utf-8",
datatype: "jsondata",
async: "true",
success: function (response) {
var msg = eval('(' + response.d + ')');
if ($('#tblResult').length != 0) {
$("#tblResult").remove();
}
var table = "<table class='tblResult' id='tblResult'><thead><tr><th>Name</th><th>Address</th><th>Age</th><th>Action</th></tr></thead> <tbody>";
for (var i = 0; i <= (msg.length - 1) ; i++) {
var row = "<tr>";
row += '<td>' + msg[i].Name + '</td>';
row += '<td>' + msg[i].Address + '</td>';
row += '<td>' + msg[i].Age + '</td>';
row += '<td><img src="edit.png" title="Edit Record." onclick="bindRecordToEdit(' + msg[i].Id + ')" /> ';
row += ' <img src="delete.png" title="Delete Record." onclick="deleteRecord(' + msg[i].Id + ')" /></td>';
row += '</tr>';
table += row;
}
table += "</tbody></table>";
$('#divData').html(table);
$('#divData').slideDown("slow");
},
error: function (response) {
alert(response.status + ' ' + response.statusText);
}
});
}
// C#
[WebMethod]
public static string bindRecordtoEdit(int id)
{
string data = string.Empty;
try
{
using (MyTestDatabaseEntities context = new MyTestDatabaseEntities())
{
var obj = (from r in context.MstNewTests select r).ToList();
JavaScriptSerializer serializer = new JavaScriptSerializer();
data = serializer.Serialize(obj);
}
return data;
}
catch (Exception)
{
return data;
}
}
首先,使用'JSON.parse(data)'而不是eval。其次,爲什麼不將表創建爲DOM對象而不是字符串連接? – some
我是JSON的新手。請給我提供一些示例 –
使用它... – some