WorknHardr
asked on
JSON Result with Backslashes?
I'm working with a jstree and it always errors with a parsorerror and illegal character error. Jstree will not tell me which character it is so I'm using the JQuery .ajax function to find the illegal charcter, it appears to be the backslash.
{"d":"[{\"data\":\"data1\" ,\"state\" :\"closed\ ",\"attr\" :{\"id\":\ "1\",\"hre f\":\"\",\ "selected\ ":false,\" rel\":\"fo lder\"},\" children\" :null}]"}
Q. How is the backslash getting inserted into the return JSON string?
{"d":"[{\"data\":\"data1\"
Q. How is the backslash getting inserted into the return JSON string?
- default.aspx -
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: '/default.aspx/GetFiles',
type: "post",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert("SUCCESS: " + JSON.stringify(data));
},
error: function (error) {
alert("ERROR: " + JSON.stringify(error));
}
});
});
- default.aspx.cs -
[WebMethod]
public static string GetFiles()
{
List<JsTreeModel> tree = new List<JsTreeModel>();
JsTreeModel node = new JsTreeModel();
node.data = "data1";
node.state = "closed";
node.attr = new JsTreeAttribute { id="1", selected = false, href="", rel = "folder" };
node.children = null;
tree.Add(node);
return tree.ToJSON();
}
public static class JSONHelper
{
public static string ToJSON(this object obj)
{
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
return serializer.Serialize(obj);
}
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER