jquery + ashx + Json 操作資料

來源:互聯網
上載者:User

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()
                {
                    //所有的都執行完後的方法
                }
            });
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.