rohit rohit - 2 months ago 10
Ajax Question

ajax function is not returning expected output

controller is returning :

return Json(new { html = "<html><body>HELLO</body></html>" },JsonRequestBehavior.AllowGet);


which is simply a string 'HELLO'

my json function is as follows :

function callPrint() {
var PrintCssContent = "";

$.ajax({
url: '@Url.Action("GetHtmlString", "Itinerary", new { area = "Travel" })',
dataType: "text",
success: function (data) {
alert(data);

WinPrint.document.write(data);
WinPrint.document.write(DivMainContent.innerHTML.toString());
WinPrint.document.write("</body></html>");
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
},
error:function(){
alert('error');
}
});
return false;
}


but json alert is returning something like this :
{"html":"\u003chtml\u003e\u003cbody\u003eHELLO\u003c/body\u003e\u003c/html\u003e"}

how to simply return 'HELLO' using my json function?

Answer

You need to use JSON.Parse to return the JSON data. Change your alert statement as,

 var parseData = JSON.parse(data);
 alert(parseData.html);

Hope this helps!.