[cgiapp] Ajax app using CAP -- return JSON or HTML?

Mark Rajcok mrajcok at gmail.com
Sat May 24 22:11:25 EDT 2008


I'm writing a new CGI app, and decided to go web 2.0 -- i.e., use Ajax
to update parts of the page, trying to never load an entirely new
page.

I'm at a crossroad:
should the CAP-based back-end normally return
  JSON data (and let the javascript library (I've decided on jQuery)
place and format the data on the page),
or should the back-end normally return
  HTML (via html templates), which will be dropped into different, say, <div>s?
(BTW, I'm not considering XML.)

It seems like the road I take will have a signficant impact on
implementation, and maybe maintainability, etc.  so I'd like to go
down the better road, if there is one.

Thinking out loud here... with JSON, I see these advantages:
- the back-end will just return data, not (HTML) document structure
- I can more easily target multiple areas of the page with a single
AJAX call (with jQuery, I don't think it is easy to target multiple
DOM elements if I return HTML)
- it will probably be easier to add visual effects to certain DOM
elements as I'm converting JSON to HTML (vs. hunting through the
returned HTML for specific elements... hmm, that would require changes
to both the back-end and the javascript -- yuck)

With HTML I see these advantages:
- less javascript to write
- the HTML can be put into templates (so the back-end logic is still
pretty much isolated from (HTML) structure), and it may be easier to
manage HTML template files, vs. one large javascript file

Comments?

And maybe it would be appropriate to do both -- sometimes return JSON,
and sometimes HTML.  Any suggested guidelines?

-- Mark


More information about the cgiapp mailing list