[Dancer-users] Ajax and Dancer

franck franck at lumberjaph.net
Thu Aug 26 11:22:14 CEST 2010


Hi!

On Thu, Aug 26, 2010 at 9:42 AM, Peter Gordon <peter at pg-consultants.com>wrote:

> 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.
>
>
I've pushed a patch in the devel branch that will, for all ajax request,
return a content type define to text/xml by default. This will be available
soon on CPAN.

Mean-while, you can do something like that:

ajax '/hello' => sub {
    header('Content-Type' => 'text/xml');
    header('Cache-Control' =>  'no-store, no-cache, must-revalidate');
    to_xml( { taconite => { replaceContent => { 'foo' => 'bar' } } } );
};

As to_xml use XML::SImple, you should check this module if you want to see
how you can make it create the structure you want. Else, you can still use
the EOD style.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.backup-manager.org/pipermail/dancer-users/attachments/20100826/0814090e/attachment.htm>


More information about the Dancer-users mailing list