jquery + ashx + Json 操作数据

1.获取DataSet
2.转成DataTable
3.
public static string DataTableToJson(DataTable dt)
    {
        StringBuilder jsonBuilder = new StringBuilder();
        //jsonBuilder.Append("{");  //输出的要求不要'}'
        //jsonBuilder.Append(dt.TableName);
        jsonBuilder.Append("[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            jsonBuilder.Append("{");
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                jsonBuilder.Append("\"");
                jsonBuilder.Append(dt.Columns[j].ColumnName);
                jsonBuilder.Append("\":\"");
                jsonBuilder.Append(dt.Rows[i][j].ToString());
                jsonBuilder.Append("\",");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("},");
        }
        jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
        jsonBuilder.Append("]");
        //jsonBuilder.Append("}");  //输出的要求不要'}'
        return jsonBuilder.ToString();
    }
4.把Json字串Response出来
5.获取Json字串

function load()     //页面加载
        {           
            $.ajax({
                type:"post",
                http://www.cnblogs.com/yongheng178/admin/%22../Handler/LinkManHandler.ashx%22,//从哪获取Json
                data:"action=getLinkMan&id="+comId,//Ajax传递的参数
               
                beforeSend:function(){
                    //获取之前显示 loading 或 正在加载
                },
               
                success:function(mess)
                {
                    $("#divImage").hide();
                    if(mess!="]")// 如果获取的数据不为空
                    {
                         var type=eval(mess);
                         var str="<a>首页</a>"
                         for(var i=0;i<type.length;i++)
                         {
                            //操作type 获取东西
                            str+="<a>"+type[i].数据库字段名+"</a>
                         }
                         $("#divLinkMan").html(str);//把str嵌入div divLinkMan 中
                     }
                     else
                     {
                        var notfind="<h2>未找到数据<a href='../Main.htm' target='_parent'>返回</a></h2>"
                        $("#divLinkMan").html(notfind);
                     }
                    
                },
               
                complete:function()
                {
                    //所有的都执行完后的方法
                }
            });
        }

你可能感兴趣的:(jquery)