0

Hii Guys!!!. I have a grid which is populating through jason data.Now I want to retrieve the jason values using jquery ajax call from server side code...

Below is my server side code...

getGriddahico.ashx

                        string json ="";
                        json = json + "{\n";
                        json = json + " \"page\":\""+intpage+"\",\n";
                        json = json + "\"total\":"+total_pages+",\n";
                        json = json + "\"records\":"+total+",\n";
                        json = json + "\"rows\": [";
                        rc = false;

                        while(rs.Read()){

                            if(rc){
                                json = json + ",";
                            }
                            json = json + "\n{";
                            json = json + "\"price\":\"" + Convert.ToInt32(rs["price"]) + "\",";
                            json = json + "\"cell\":[" + Convert.ToInt32(rs["price"]) + "";
                            json = json + ",\"" + Convert.ToString(rs["username"]) + "\"";
                            json = json + ",\"" + Convert.ToString(rs["ordinal"]) + "\"";
                            json = json + ",\"" + Convert.ToString(rs["toc"]) + "\"]";
                            json = json + "}";

                            rc=true;
                        }
                        json = json +"]\n";

                        json = json +"}";

and Here is my Jquery Ajax Call code.

 $(document).ready(function () {
        $.getJSON('getGriddahico.ashx', function (data) {
            //loop thru  json data
            //data will contain  json values < --
            $.each(data, function (key, val) {
                console.log(val);
            });
        });
    });

Plz guys Help me how to retrive the value from server side datareader to client side variable.. Thanx in advance.

vikas
  • 101
  • 1
  • 3
  • 16

2 Answers2

2

To me it looks as though you are returning a string value which you need to then parse in the JavaScript into a json object.

Use the code $.parseJSON()

$.getJSON('getGriddahico.ashx', function (data) {

    /* convert to json object */
    var json $.parseJSON(data);

    //loop thru  json data
    //data will contain  json values
    $.each(json, function (key, val) {
        console.log(val);
    });
});
Tim B James
  • 20,084
  • 4
  • 73
  • 103
0

as you are getting -2147483648 -2147483648 150508 [] the fourth one ([]) is an array.
so you will have to loop again for getting values from the inner array.

You need multiple loop for the same. I think below should work.
In the first loop you will get the values which are not array
and In the inner loop you will get the array values.
Values of page,total and records in the first loop
and in the inner loop you will get price and cell

$(document).ready(function () {
    $.getJSON('getGriddahico.ashx', function (data){ 
        $.each(data, function(idx, obj){ 
           $.each(obj, function(key, value){
                console.log(key + ": " + value);
           });
    });
});

You can refer this link

looping over a json object array with jquery

Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117
  • @krshekar Thnx sir for ur response but here also I am getting only random values like 0:- 1:2 2:1 3:4 etc – vikas Jan 30 '13 at 11:44
  • can you put your json created by c# – शेखर Jan 30 '13 at 11:48
  • @krshekar Here is my json values Sir... { "page":"1", "total":1506, "records":150508, "rows": [ {"price":"0","cell":[0,"","3011","0","3011","9999","1784","6/13/2011 12:00:00 AM","1/1/1900 1:22:17 PM","63","","0","INTERNAL"]}, {"price":"0","cell":[0,"","1229","0","1229","9999","1255","6/13/2011 12:00:00 AM","1/1/1900 1:26:23 PM","2","","0","INTERNAL"]}] } – vikas Jan 30 '13 at 11:50
  • what do you want to do with these values? – शेखर Jan 30 '13 at 11:53
  • These values are generated at server i want to retrieve these values to .aspx client page..by jquery url call – vikas Jan 30 '13 at 11:55
  • I mean what do you want to do with these values display some where in the page ? – शेखर Jan 30 '13 at 13:52