Edit: I've managed to get it working by setting the WebMethod to a void rather than a string and then writing to the response manually:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
HttpContext.Current.Response.Write(json);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
I have a webmethod which returns a serialised json object, below is an example:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string PostDropDowns()
{
TestClass t = new TestClass()
{
abc = "this has been sent over",
Strings = new List<string> { "fdfdfd", "fdfdfdf" }
};
var json = JsonConvert.SerializeObject(t);
return json;
}
The JSON text =
{"abc":"this has been sent over","Strings":["fdfdfd","fdfdfdf"]}
In my c# UWP application I am calling this webrequest
public static void GetDropDowns(string address)
{
//create the HTTPWebRequest
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
request.Method = "POST";
request.ContinueTimeout = 20000;
// request.Accept = "text/json";
request.ContentType = "application/json; charset=utf-8";
try
{
//get the response
using (var response = request.GetResponseAsync().Result)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
var responseFromServer = reader.ReadToEnd();
var dds = JsonConvert.DeserializeObject<TestClass>(responseFromServer);
}
}
catch (Exception e)
{
//error handling in here
}
}
However, the json I am getting back looks like this:
{"d":"{\"abc\":\"this has been sent over\",\"Strings\":[\"fdfdfd\",\"fdfdfdf\"]}"}
So when deserialize back to my TestClass both Title and Strings are null.
How do I preve that d coming through or get round it?
Thanks in advance