I plan to cancel the traditional XML and front-end data transmission on my personal website. Use JSON Data Format
This is part of my front-end: var option = {
Parameters :"",
Method: "Get ",
Onsuccess: function (Transport ){
Rp = transport. responsetext;
VaR JSON = eval ('+ RP + ')');
For (VAR I = 0; I <JSON. datatable. length; I ++)
{
Taskmenu2.add (New taskmenuitem (JSON. datatable [I]. lifecol, "image/friends.gif", "parent. Window. Frames [1]. Location. href = 'redirect. aspx? Id = "+ JSON. datatable [I]. ID + "'"));
}
},
Onfailure: function (Transport ){
Alert (transport. responsetext + "data submission failed. Check the network or try again. ");
}
};
VaR request = new Ajax. Request ("Ajax. aspx", option );
Taskmenu2 is a self-defined JavaScript class. Ignore it:
Below is. Ajax. aspx. CS page if (! This. ispostback)
{
Sqldatareader SDR = sqlhelper. executereader (commandtype. Text, "select * From readlife", null );
Datatable dt = dB. convertdatareadertodatatable (SDR );
Response. Write (db. datatabletojson (DT ));
}
Here we use a method:
There is a static method in the DB class: 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 ();
}