rohit
rohit

Reputation: 67

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?

Upvotes: 0

Views: 48

Answers (2)

Ameen Som
Ameen Som

Reputation: 11

You need to convert the result to json, then print it out, here is the code:

function callPrint() {
    var PrintCssContent = "";

    $.ajax({
        url: '@Url.Action("GetHtmlString", "Itinerary", new { area = "Travel" })',
        dataType: "text",
        success: function (data) {
            var dataParsed = JSON.parse(data);
            WinPrint.document.write("<html><body>");
            WinPrint.document.write(dataParsed.html);
            // 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;
}

Upvotes: 1

David R
David R

Reputation: 15639

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!.

Upvotes: 1

Related Questions