What I want to do:
I have an application based on symfony2 (php). A simple action returns data as json like this
$headers = array(
'Content-Type' => 'application/json'
);
return new Response($data, 200, $headers);
This action is called from javascript like this
function loadData(action){
$.ajax({
type: "POST",
url: action,
success: function(data){
console.log(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
So this seems very basic so far. From the console I can see the correct data returned. This data should be placed somewhere on the project website.
Question:
Is there any straight forward way to create html from json within javascript? Actually I want seperate templates (twig) from the logic of the project. So would it be correct to generate the html e.g. in the success callback in the loadData method?
I could bypass this problem by returning html instead of json. But I think about building some kind of rest api for my project which I think required json for transport.
Any suggestions or ideas? Please share your ways of handling this.