Reputation: 45
I have a JSP and a servlet. In the JSP i am giving a ajax call to the servlet. The servlet calls the REST API and gets me data. So, i get JSON data in the servlet from the REST API. Which i am formatting using,
json.serialize(true);
The jsondata is formatted. Now, I want to show this json data as it is i.e. in formatted form. So i send it to the frontend.
pw.write(myformattedjsontext)
where i have the code,
var xhrDetailsArgs={
handleAs: "text",
sync: true,
load: function(data)
{
document.getElementById("DetailsGrid").innerHTML = data + "";
},
error: function(error)
{
alert("Error while loading details"+error);
}
}
But i get unformatted data here. The format is lost.I have to get the formatted data here. Please Help!!!
Upvotes: 0
Views: 129
Reputation: 734
If you change your handleAs attribute to "json", you will have an object in your data variable. You can use
JSON.stringify(data, null, " ");
to get a formatted json string.
Upvotes: 1