fehays fehays - 1 month ago 17
Ajax Question

How can I return a JArray from a [WebMethod] using Json.NET?

Is it possible to return a

JArray
object from a
[WebMethod]
using jQuery ajax and Json.NET? Example:

[WebMethod]
public JArray GetJsonData()
{
JArray jArray = new JArray();
JObject jObject = new JObject();
jObject.Add(new JProperty("name", "value"));
jArray.Add(jObject);
return jArray;
}


When I call this using jQuery ajax, it returns something that looks like this:

[[[[]]]]

Answer

I believe you need to return a string.

[WebMethod]
public string GetJsonData()
{
    JArray jArray = new JArray();
    JObject jObject = new JObject();
    jObject.Add(new JProperty("name", "value"));
    jArray.Add(jObject);
    return jArray.ToString();
}
Comments