[Catalyst] implementing ajax

Ashley apv at sedition.com
Mon Mar 10 16:55:22 GMT 2008


On Mar 10, 2008, at 9:37 AM, Jennifer Ahn wrote:
> hello!
>
> i'm wondering how one would implement the server side controller  
> end of an application with ajax.  i have the javascript on my  
> client sending an xml file to a uri controller method but, how does  
> the controller process that xml?  is the standard way of doing this  
> by reading in the xml file through stdin?  i'd like to implement  
> this without using JSON of jemplate.

Ajax works behind the scenes like any CGI-ish interaction. Your  
question is a bit vague so the sample code is too, but you could do  
something like this-

use XML::LibXML;

sub ajax_widget : Public {
     my ( $self, $c, $skin ) = @_;
     my $parser = XML::LibXML->new();

     # get it out of a XHR POST; badly formed or missing XML will  
cause an error
     my $doc = $parser->parse_string($c->request->body_params-> 
{xml_sent_by_js});

     # use $doc and its contents however you like

     $c->forward("View::YourAjaxView"); # return whatever your JS  
expects
}

JSON is generally easier to work with, so I say, if you have a choice  
going forward.

-Ashley




More information about the Catalyst mailing list