troubleshooting Question

JSON Result with Backslashes?

Avatar of WorknHardr
WorknHardr asked on
ASP.NETjQueryAJAX
2 Comments1 Solution1741 ViewsLast Modified:
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\",\"href\":\"\",\"selected\":false,\"rel\":\"folder\"},\"children\":null}]"}

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);
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
leakim971
Multitechnician
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros