[Dancer-users] Ajax and Dancer

Peter Gordon peter at pg-consultants.com
Thu Aug 26 09:42:58 CEST 2010


I am trying to use Ajax, and javascript is expecting a content-type of text/xml.
I have come up with the following pattern, which is not very pretty.


ajax '/hello' => sub {
    my $data=<<'EOD';
    <taconite> 
	<replaceContent select=".target"> 
           NEW DATA
        </replaceContent> 
    </taconite> 
EOD
    return Dancer::Response->new(
        content      => $data,
        headers => [ 
		    'Cache-Control' =>	'no-store, no-cache, must-revalidate',
		    'Keep-Alive' =>	'timeout=5, max=99',
		    'Connection'	=> 'Keep-Alive',
		    'Transfer-Encoding'	=> 'chunked',
		    'Content-Type'	=> 'text/xml; charset=UTF-8',
		]
    );

I would pefer something like this:

ajax '/hello' => sub {

    my $data=<<'EOD';
    <taconite> 
	<replaceContent select=".target"> 
           NEW DATA
        </replaceContent> 
    </taconite> 
EOD
	return $data ; 
}

What would be the best/nicest way of doing this? 
Dancer::Route uses the global content_type, but that would seem to affect all content_types.
A related question is: How can I set context related information.

Peter



More information about the Dancer-users mailing list