【问题标题】:How to Retrieve json data using ajax call in asp.net如何在 asp.net 中使用 ajax 调用检索 json 数据
【发布时间】:2013-01-30 05:36:19
【问题描述】:

我正在使用 JQgrid 来显示数据库记录。现在根据我的需要,我将请求传递给处理程序文件以使用 jquery ajax 调用检索数据。但是除了 jason 数据字段之外,所有其他数据字段都将出现。 下面我发布我的 .handler 和 .aspx 代码..

来自服务器代码的.handler 文件..

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

这是我的 .aspx 代码..

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

                });
            }
        });

但在警报框中,我得到以下数据字段...

{
  "page":"-2147483648",
    "total":-2147483648,
       "records":150508,
          "rows":[]
 }

【问题讨论】:

  • 什么是rs?虽然我不知道,但我很肯定必须有更好的方法将数据编码为 JSON
  • @ExplosionPills thnx 先生,您的快速回复“rs”是“DataReader”

标签: jquery asp.net ajax json


【解决方案1】:

有一些错别字,你可以试试这个:

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

       });
    });

【讨论】:

  • var $price = jason.price console.log(jason);
  • $.each(data, function (i, jason) { var v = jason.price; alert(v); 这没有给出任何价值先生
【解决方案2】:

Vikas,创建一个对象来存储您的数据读取器数据,例如

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

希望这能解决您的问题。

【讨论】:

  • Thnx 先生,先生,我正在创建 jason 数据格式,我需要将其原样发送到浏览器页面
【解决方案3】:

你可以这样走

 $(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;
                  }
               }

            });
        }
    });

您也可以通过此链接。
convert from SqlDataReader to JSON
http://www.west-wind.com/weblog/posts/2009/Apr/24/JSON-Serialization-of-a-DataReader

我会建议你使用带有 json 的 jtamplate

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

【讨论】:

  • @vikas 您正在从数据阅读器创建json?我对吗。该链接是相同的。
  • @vikas 我建议你使用 jtamplate 来有效地使用 jason
猜你喜欢
  • 2013-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
  • 2016-08-16
  • 1970-01-01
相关资源
最近更新 更多