Front Page
Copy Code code as follows:
$.ajax ({
Type: "Post",
ContentType: "Application/json",
URL: ".. /webform1.aspx/getrightsstr ",
DataType: "JSON",
Success:function (msg) {
alert (MSG.D);
var data = eval ("+ MSG.D +"));
$.each (data.rights, function (index, item) {
Alert (item. Rightsname);
});
},
Error:function (E, S, D) {
Alert (e);
alert (s);
alert (d);
}
});
Background method:
Copy Code code as follows:
<summary>
The DataTable turns into JSON
</summary>
<param name= "Jsonname" >josn name </param>
<param name= "DT" > Data set to be converted </param>
<returns></returns>
public static string Datatabletojson (String jsonname, DataTable DT)
{
StringBuilder Json = new StringBuilder ();
Json.append ("{\" "+ Jsonname +" \ ": [");
if (dt. Rows.Count > 0)
{
for (int i = 0; i < dt. Rows.Count; i++)
{
Json.append ("{");
for (int j = 0; j < dt.) Columns.count; J + +)
{
Json.append ("\" "+ dt.) COLUMNS[J]. Columnname.tostring () + "\": \ "" + dt. ROWS[I][J]. ToString () + "\" ");
if (J < dt. COLUMNS.COUNT-1)
{
Json.append (",");
}
}
Json.append ("}");
if (i < dt. ROWS.COUNT-1)
{
Json.append (",");
}
}
}
Json.append ("]}");
return json.tostring ();
}
This method is a way to convert a DataTable into a string. The problem encountered today is in var data = eval ("+ MSG.D +"));
Error prompted is missing} This method has been useful for many times without knowing why such a mistake occurred.
Looking for a long time, only to find that the problem is the data in the returned string has "\" the character, "\" has the function of escaping
All will appear this error, Halo Ah, the real harm to me, I hope you do not like me so confused.