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

Bruce McKenzie bruce at 2MinuteExplainer.com
Mon May 26 18:48:55 EDT 2008


Mark Rajcok wrote in an email message dated 5/24/2008 10:11 PM:
> 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.
> 
> 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 is easy. Just turn the html into a jQuery object (a pseudo-Array). 
The ajax call should include options like these:

dataType: "html",
success: function(html){
// return from ajax is html because you specified that dataType
    var $myDivs = $(html);
    $('#someDiv').html( $myDivs.eq(0) );
    $('#anotherDiv').replace( $myDivs.eq(1) );
   //etc
}

HTH

Bruce
-- 
Bruce McKenzie
http://www.2MinuteExplainer.com


More information about the cgiapp mailing list