简体   繁体   English

从Ajax response(Json)错误构建表行:“无效字符”

[英]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.

相关问题 json ajax调用以200 OK返回成功,但由于无效字符而出错 - json ajax call returning success with 200 OK but goes to error with Invalid character JSON解析错误 - 位置0处的无效字符 - JSON Parsing error -Invalid Character at position 0 以编程方式构建表以从JSON导出/导入? - Build table programmatically for export to/import from JSON? 从asp.net中的用户控制页面进行ajax调用时,出现“ SyntaxError:JSON.parse:意外字符”错误 - getting“SyntaxError: JSON.parse: unexpected character” error while make ajax call from user control page in asp.net DataTables警告:表格ID = bootstrap-data-table-无效的JSON响应-DataTables JS插件 - DataTables warning: table id=bootstrap-data-table - Invalid JSON response - DataTables JS Plugin JSON响应的格式无效? - Invalid format of JSON-response? 无效的JSON响应jquery数据表 - Invalid JSON response jquery datatable 无效的JSON响应MVC API - Invalid JSON Response MVC API 无法从使用json和ajax调用的webservice方法获得响应 - unable to get response from a webservice method using json and ajax call SSAS 表格连接错误 - 服务器发送了无法识别的响应,XmlException 0x06 是无效字符 - SSAS Tabular connection error - The server sent an unrecognizable response, XmlException 0x06 is an invalid character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM