[英]build table rows from Ajax response(Json) error: 'Invalid Character'
I'm successfully getting the response as jSon from my code behind c#. 我已经从c#背后的代码中成功获得了jSon的响应。 While converting jSon to table in jQuery it throwing error('Invalid Character').
在jQuery中将jSon转换为表时,会抛出错误(“无效字符”)。 here is my code:
这是我的代码:
$('#reports').click(function () {
var pageUrl = '<%= ResolveUrl("~/Admin.aspx/SystemstatusReport")%>';
$.ajax({
type: 'POST',
url: pageUrl,
contentType: 'application/json; charset=utf-8',
success: function (data) {
var response = $.parseJSON(data);
$(function () {
$.each(response, function (i, item) {
var $tr = $('<tr>').append(
$('<td>').text(item.SystemID),
$('<td>').text(item.SystemIP),
$('<td>').text(item.SystemStatus)
);
console.log($tr.wrap('<p>').html());
});
});
},
error: function (data, success, error) {
alert("Error:" + error);
}
});
});
My c# [WebMethod] 我的c#[WebMethod]
public static string SystemstatusReport()
{
using (OleDbConnection con=new OleDbConnection( conStr))
{
con.Open();
string query = "SELECT * FROM SystemStatus";
using (OleDbDataAdapter da=new OleDbDataAdapter( query,con))
{
DataSet ds=new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
var jsonString = DataTableToJson(dt);
return jsonString;
}
}
}
Edit: 编辑:
My json response: 我的json回应:
[{ "SystemId": 1753259, "IP": "1855652", "SystemStatus": "online" },{ "SystemId": 1753359, "IP": "1585652", "SystemStatus": "online" }]
It should be data.d
as shown below: 它应该是
data.d
,如下所示:
var response = $.parseJSON(data.d);
Because C# => 3.5 serialize all JSON responses into a variable d. 因为C#=> 3.5将所有JSON响应序列化为变量d。
You need to change your code as below: 您需要按以下方式更改代码:
$(function () {
$.each(response, function (i, item) {
var $tr = $('<tr>').append(
$('<td>').text(item.SystemId),
$('<td>').text(item.IP),
$('<td>').text(item.SystemStatus)
);
console.log($tr.wrap('<p>').html());
});
});
You need to give exact name as it is in Json response. 您需要提供准确的名称,就像在Json响应中一样。
Demo: http://jsfiddle.net/Rj9bR/57/ 演示: http : //jsfiddle.net/Rj9bR/57/
I used JSON.parse method to get the json string. 我使用JSON.parse方法获取json字符串。 Below is my code
下面是我的代码
success: function (data) {
var sam = JSON.parse(data);
$(function () {
var trHTML = '';
for (var i = 0; i < sam.length; i++) {
trHTML += '<tr><td>' + sam[i].IP + '</td><td>' + sam[i].SystemID + '</td><td>' + sam[i].Bay + '</td></tr>';
}$('#records_table').append(trHTML);
});
});
And also I forgot to mention index of the JSON object. 而且我也忘了提到JSON对象的索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.