0

I am using JQgrid to display database records .Now as per My need I am passing request to handler file to retrieve data using jquery ajax call..But all other data fields are coming except jason datafield.. Below I am posting my .handler and .aspx code..

.handler file from server code..

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["authcode"]) + "\"";
                            json = json + ",\"" + Convert.ToString(rs["extension"]) + "\"";
                            json = json + ",\"" + Convert.ToString(rs["trunk"]) + "\"";
                            json = json + ",\"" + Convert.ToString(rs["dialnumber"]) + "\"";
                            json = json + ",\"" + Convert.ToString(rs["dialdate"]) + "\"";
                            json = json + ",\"" + Convert.ToString(rs["dialtime"]) + "\"";
                            json = json + ",\"" + Convert.ToString(rs["duration"]) + "\"";
                            json = json + ",\"" + Convert.ToString(rs["destination"]) + "\"";
                            json = json + ",\"" + Convert.ToString(rs["price"]) + "\"";
                            json = json + ",\"" + Convert.ToString(rs["toc"]) + "\"]";
                            json = json + "}";

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

                        json = json +"}";


                        HttpContext.Current.Response.Write(json);

And here is my .aspx code..

 var jason;
       $(document).ready(function() {
            {
                var URL='getGriddahico.ashx';
                $.ajax({
                   url:URL,
                   type:'GET',
                   //datatype:'jason',
                   success: function (data) {
                       jason = data;
                      // alert(jason);
                   }

                });
            }
        });

But in alert Box I am getting following data fields...

{
  "page":"-2147483648",
    "total":-2147483648,
       "records":150508,
          "rows":[]
 }
vikas
  • 101
  • 1
  • 3
  • 16

3 Answers3

0

There are some typos, you can try this:

   $(document).ready(function() {
       var URL='getGriddahico.ashx';
       $.ajax({
          url:URL,
          type:'GET',
          datatype:'json',
          success: function (data) {
              $.each(data, function(i, jason){
                  console.log(jason.price); // <--should print your price
              });
          }

       });
    });
Jai
  • 74,255
  • 12
  • 74
  • 103
  • `var $price = jason.price` <-- $price will hold the price and if you want to hold all the response then `console.log(jason);` – Jai Jan 30 '13 at 06:02
  • $.each(data, function (i, jason) { var v = jason.price; alert(v); this is not giving any value sir – vikas Jan 30 '13 at 07:33
0

Vikas, create a object which stores your data reader data like

List<cutomobject> result = new List<customobject>();
while(rs.Read()){
       result.add(new customobject{ price= "Convert.ToInt32(rs["price"])", cell = "Convert.ToInt32(rs["cell"])" ,... });
              }
            var jsonData = new
                {
                    total = result.Count() / 15,
                    page = 1,
                    records = result.Count(),
                    rows = result
                };
                 HttpContext.Current.Response.Write(json);

Hope this will solve your problem.

Meraj
  • 426
  • 3
  • 10
  • 22
  • Thnx sir ,Sir i am creating jason dataformate and i need to send this as it is to the browser page – vikas Jan 30 '13 at 05:58
0

You can go like this

 $(document).ready(function() {
 {
            var URL='getGriddahico.ashx';
            $.ajax({
               url:URL,
               type:'GET',
               datatype:'jason',
               success: function (data) {
                  for (var i = 0; i < data.length; i++) 
                  {
                      data[i].price;
                      data[i].cell;
                  }
               }

            });
        }
    });

You can also go through this link.
convert from SqlDataReader to JSON
http://www.west-wind.com/weblog/posts/2009/Apr/24/JSON-Serialization-of-a-DataReader

I will suggest you to use jtamplate with json

http://blog.jambura.com/2011/12/18/jquery-json-and-jtemplates-for-ajax-driven-web-app/

Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117