0

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("<div>" + data.toString() + " </div>").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?

Daarwin
  • 2,896
  • 7
  • 39
  • 69

3 Answers3

0

You could return your data as JSON object:

var outputObject = "{ html = '" + jSonTestResultReport  + "' + someOtherData = ... + "}";
context.Response.Write(outputObject);

and access them in success function like this:

data.html and data.someOtherData
berliner
  • 1,887
  • 3
  • 15
  • 23
  • Its not working. I get the error in a Alert popoup: Syntax error. Unexpected token = The output i send back is: { "resultReport" = "
  • js
  • " } – Daarwin Sep 26 '12 at 13:25
  • Oh i figured it out. I used an = instead of an : – Daarwin Sep 26 '12 at 13:53