Friday, 2 June 2017

asp.net - Returning Json to javascript

Answer


I make a Json call to an ashx handler like this:



    attributes = $("#pageHtmlTag").attr("class").trim();
urlToHandler = 'JSonTestHandler.ashx';
jsonData = '{' + attributes + '}';

$.ajax({
url: urlToHandler,
data: jsonData,

dataType: 'json',
type: 'POST',
contentType: 'application/json',
success: function (data) {
setAutocompleteData(data.responseDateTime);
$("body").add("
" + data.toString() + "
").appendTo(document.body);
alert("grate suceees");
},
error: function (data, status, jqXHR) {
alert('There was an error.' + jqXHR);

}
}); // end $.ajax


I receieve it and proccess it. I also want to send back some HTML to be displayed but i dont know how to send html back to the Jscript.



ashx:



    string jsonData = new StreamReader(context.Request.InputStream, System.Text.Encoding.UTF8).ReadToEnd();


.............................

var testResultReportString = testResultReport.GetReportHtml();

var serializer = new JavaScriptSerializer();
var jSonTestResultReport = serializer.Serialize(testResultReportString);

context.Response.Write(jSonTestResultReport);



So the question is. How do i return data to the Ajax calls success function?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...