I am really new to using JSON to handle the Ajax Request and Response loop. I previously used only the old parameters passed as POST data, and I displayed direct HTML in the response, which was then placed in the DOM. As I looked at various examples and read various tutorials, it seems pretty common practice to just build a string from a JSON object mixed with HTML that was hard-coded into a string, and then assign the string as innerHTML to some element.
A general example looks something like this:
var jo = eval(req.responseText); var strTxt = '<span>' + jo.f_name + ' ' + jo.l_name + '</span><br/>' + 'Your Age Is: ' + jo.age + '<br/>'; $('myDiv').innerHTML = strTxt;
Is there a more elegant (or correct) way to handle a JSON response so that I don't have hardcoded HTML in javascript? Or is it pretty much how people do it?
PS Links to textbooks or other sources are welcome.
source share