Embedding HTML vs using JSON
Hi, I'm just confused with this. When requesting some data from the server using ajax, I'm having a difficulty deciding whether I should inject the code into the DOM directly or should I use JSON to wrap the data first and parse the response before accessing the data i need.
I've seen that when you append some html code into your code the script elements contained in the html gets executed correctly. For example i use the code below to request some data from the server and append the response to the body.
-
$.get('http://some/url',
function(response) {
$('body').append(response);
}
);
and for example the response is:
-
<span>Some Content</span>
<script type="text/javascript">
alert('the server said hello to you');
</script>
the html content will be appended to the end of the body and the script issuing alert will be executed.
on the other hand, i could return a json respone but i still have to parse it
-
["html":"<span>Some Content</span>",
"script":"<script type="text/javascript">
alert('the server said hello to you');
</script>"]
so what do you think? would appending the content directly be enough?
thanks.