Front page
$.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:
///
/// Convert DataTable to Json
/// ///
josn name
///
Dataset to be converted
/ //
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 method to convert DataTable into a string. The problem I encountered today was var data = eval("(" msg.d ")");
An error occurred. The prompt was missing }. This method has been used many times. I don’t know why such an error occurred.
After searching for a long time, I found out that it was a problem with the data. There is the character "" in the returned string, and "" has the function of escaping.
This error will appear in all cases. I feel dizzy. It really hurts me. I hope you won't be as confused as me.