Jquery Step01-$.Ajax Json

来源:互联网 发布:广州 php软件开发公司 编辑:程序博客网 时间:2024/05/16 14:27

 <script>
    //i是索引 n是内容
        function Ajax()
        {
              var tbody="";
              $.ajax({
              type: "GET",//用POST方式传输
              dataType:"json",//数据格式:JSON
              url:'Handler.ashx',//目标地址
              data:"orderby=1",
              beforeSend:function(){$("#divload").show();$("#Pagination").hide();},//发送数据之前
              complete:function(){$("#divload").hide();$("#Pagination").show()},//接收数据完毕
              success:function(data) { 
                    $("#productTable tr:gt(0)").remove();//?????????????????????????
                       // $("#productTable tr").remove();  //删除原有的行
                        var productData = data.Products;//创建对象
                        $.each(productData,function(i, n) {
                            var trs = "";
                            trs += "<tr > <td>" + n.ID + " </td> <td>" + n.Name + " </td>  </tr>";
                            tbody += trs;
                        });
                        $('#productTable').append(tbody);
                        alert( $('#productTable').html());
                        $("#productTable tr:gt(0):odd").attr("class", "odd");
                        $("#productTable tr:gt(0):even").attr("class", "enen");
                       
                        $("#productTable tr:gt(0)").hover(function(){
                            $(this).addClass('mouseover');
                        },function(){
                            $(this).removeClass('mouseover');
                        });
                }
               
             });

        }
       
        $(document).ready(function(){
            $("#Button1").click(function(){
                Ajax();
           
            });
        });
       
           function AddTr(id){
            
                tr_id = $("#test>tbody>tr:last").attr("id");
                tr_id++;  $("#"+id+" tr:gt(0)").remove();
                //alert(tr_id);
                str = "<tr id = '"+tr_id+"'><td width='30%'>re1</td><td width='30%'>re2</td><td width='30%'>re3</td></tr>";
                $('#'+id).append(str);
            }
   
    </script>

</head>
<body>
--------------

 

 StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        string dtName = "Products";
  
        DataSet ds = new DataSet();
        DataTable dt = new DataTable(dtName);
        ds.Tables.Add(dt);

        DataColumn dc1 = new DataColumn("ID", Type.GetType("System.String"));
        dt.Columns.Add(dc1);
        DataColumn dc2 = new DataColumn("Name", Type.GetType("System.String"));
        dt.Columns.Add(dc2);

       
        for (int i = 1; i <= 10; i++)
        {
            DataRow dr = ds.Tables[0].NewRow();
            dr["ID"] = i.ToString();
            dr["Name"] = "这里是Name" + i.ToString();
            ds.Tables[0].Rows.Add(dr);
        }
        using (JsonWriter jw = new JsonWriter(sw))
        {
            JsonSerializer ser = new JsonSerializer();
            jw.WriteStartObject();
            jw.WritePropertyName(dtName);
            jw.WriteStartArray();
            foreach (DataRow cdr in dt.Rows)
            {
                jw.WriteStartObject();

                foreach (DataColumn dc in dt.Columns)
                {
                    jw.WritePropertyName(dc.ColumnName);
                    ser.Serialize(jw, cdr[dc].ToString());
                }

                jw.WriteEndObject();
            }
            jw.WriteEndArray();
            jw.WriteEndObject();

            sw.Close();
            jw.Close();

        }

        //return sb.ToString();

        json.Response.Write(sb.ToString());

        json.Response.End();

Json就是一种序列化