This is very simple.I think i may be having an issue with incompatible response content type
This is the code i am using to send a jQuery ajax request:
var settings = { dataType: 'json',
url: 'services/loadTemplate.ashx',
data: JSON.stringify({ 'Name': element.name }), // element.name },
processData: false,
//contentType: "application/json; charset=utf-8",
type: 'POST',
success: function (data) {
console.log('successData:'+data);
alert(data);
},
error: function (xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
}
$.ajax(settings);
and this is the response i am trying to fetch in success callback.
{
'name': 'sMan',
'svg1': 'Created with RaphaëlText',
'svg2': ''
}
But success is not fired.I am setting context.Response.ContentType = "application/json"; in the server side request handler and am simply writing the above string to response.Whats the correct way to do this?
EDIT:
returning it this way:
return "{'name':'" + name + @"',
'svg1':'" + dt.Rows[0]["svg1"].ToString() + @"',
'svg2':'" + dt.Rows[0]["svg2"].ToString() + "'}";
This worked:
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
{
jsonWriter.Formatting = Formatting.Indented;
// jsonWriter.WriteStartArray();
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("name");
jsonWriter.WriteValue(name);
jsonWriter.WritePropertyName("svg1");
jsonWriter.WriteValue(dt.Rows[0]["svg1"].ToString());
jsonWriter.WritePropertyName("svg2");
jsonWriter.WriteValue(dt.Rows[0]["svg2"].ToString());
jsonWriter.WriteEndObject();
/* return "{'name':'" + name + @"',
'svg1':'" + dt.Rows[0]["svg1"].ToString() + @"',
'svg2':'" + dt.Rows[0]["svg2"].ToString() + "'}";*/
}
return sb.ToString();