[Dancer-users] ajax method, what? why? how?

franck franck at lumberjaph.net
Thu Aug 26 22:41:12 CEST 2010


so, for the reasons. Let's say you have a path, like '/user/punk.kish'. If
this page is requested from a browser, you want to return a full page, with
layout and HTML. But you may also want to be able to call this same url from
a javascript query, using ajax. So instead of having

get '/user/:user' => sub {
     if ($request_is_from_ajax) {
         # create xml, set headers to text/xml, blablabla
     }else{
         template users, {....}
     }
};

you can have

get '/user/:user' => sub {
    template users, {...}
}

and

ajax '/user/:user' => sub {
     to_xml({...});
}

because it's an ajax query, you know you need to return xml content, and the
content type of the response is set for you.

You may not need this feature. As you said, it just a 'get' query, so you
don't "have" to use this keyword, and yes, it's broken in current release.
But I've been working all this evening on this fix, and it will work again
as expected in the next release.

I hope it's more clear now why this can be useful :)

On Thu, Aug 26, 2010 at 10:30 PM, P Kishor <punk.kish at gmail.com> wrote:

> On Thu, Aug 26, 2010 at 3:22 PM, franck <franck at lumberjaph.net> wrote:
> > Hi
> > I think I've found the reason why it doesn't works.
>
> Hi franck, I guess you need add a reason as to why this method is
> needed. As I said, once it didn't work, I just used get, and that
> worked well. As far as the server (Dancer) is concerned, an incoming
> call is either a get/post/delete, etc. The ajax-ness of it is that it
> has been initiated as an ajax requested, and that is a client side
> issue. So, I use jQuery to make a call, and that call comes to the
> server as a get request. So, why is the ajax method needed on the
> server? What does it do that get can't or won't do?
>
>
> > There is a patch in the
> > "devel" branch. I need to add more tests, if everything works as
> expected,
> > it will be released really soon.
> >
> > On Sat, Aug 21, 2010 at 3:23 PM, P Kishor <punk.kish at gmail.com> wrote:
> >>
> >> I can't figure out how the ajax method works. In my JavaScript (with
> >> jQuery), I have
> >>
> >>    $.ajax({
> >>        beforeSend: function(xhr){
> >>            xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
> >>        },
> >>        url: '/model/' + model_id,
> >>        dataType: "json",
> >>        success: function(data) {
> >>
> >>            // do something with
> >>        }
> >>    });
> >>
> >> and, in my App.pm, I have
> >>
> >>    ajax '/model/:model_id' => sub {
> >>        my $result = do something with model_id
> >>        return to_json($result);
> >>    }
> >>
> >> however, the above doesn't work at all. I get a 404. If I change the
> >> ajax method to
> >>
> >>    get '/model/:model_id' => sub {}
> >>
> >> then it works fine. Which brings me to the question -- what is this
> >> ajax method supposed to do better than get or post? And, why doesn't
> >> it work for me?
> >>
> >>
> >>
> >> --
>
>
>
>
> --
> Puneet Kishor http://www.punkish.org
> Carbon Model http://carbonmodel.org
> Charter Member, Open Source Geospatial Foundation http://www.osgeo.org
> Science Commons Fellow, http://sciencecommons.org/about/whoweare/kishor
> Nelson Institute, UW-Madison http://www.nelson.wisc.edu
> -----------------------------------------------------------------------
> Assertions are politics; backing up assertions with evidence is science
> =======================================================================
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.backup-manager.org/pipermail/dancer-users/attachments/20100826/5c5c57a1/attachment.htm>


More information about the Dancer-users mailing list