Hi!

On Thu, Aug 26, 2010 at 9:42 AM, Peter Gordon <peter@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.