0

I'm successfully getting the response as jSon from my code behind c#. While converting jSon to table in jQuery it throwing error('Invalid Character'). 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]

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:

[{ "SystemId": 1753259, "IP": "1855652", "SystemStatus": "online" },{ "SystemId": 1753359, "IP": "1585652", "SystemStatus": "online" }]
2
  • Can you console.log(data) and paste the output? It could be helpful.
    – michelem
    Sep 2, 2015 at 10:44
  • Hi @Michelem I edited the question Sep 2, 2015 at 11:00

2 Answers 2

0

It should be data.d as shown below:

 var response = $.parseJSON(data.d);

Because C# => 3.5 serialize all JSON responses into a variable 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.

Demo: http://jsfiddle.net/Rj9bR/57/

14
  • I changed that line. Now it's not showing any error. But it's not showing any results? Sep 2, 2015 at 10:55
  • same result!. Can you just write an example in the same fiddle? Sep 2, 2015 at 11:15
  • it is showing <td>1753259</td><td>1855652</td><td>online</td> (index):34 <td>1753359</td><td>1585652</td><td>online</td>
    – Neel
    Sep 2, 2015 at 11:32
  • plz tell what type of result you want @SarathJasrin
    – Neel
    Sep 2, 2015 at 11:33
  • SytemId, SystemIP, SystemStaus these 3 are headers others are values. I want to create a table. Sep 2, 2015 at 11:40
0

I used JSON.parse method to get the json string. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.