简体   繁体   中英

How to Retrieve json data using ajax call in asp.net

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":[]
 }

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
              });
          }

       });
    });

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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM