Problem with Dancer::Plugin::Ajax under Plack
I am trying to run multiple Dancer apps under Plack, One of my routes is an "ajax" route using Dancer::Plugin::Ajax My $app inside the psgi file is defined as: my $app = sub { local $ENV{DANCER_APPDIR} = 'MyAppDir'; setting appdir => 'MyAppDir'; load_app "MyApp"; Dancer::App->set_running_app('MyApp'); Dancer::Config->load; my $request = Dancer::Request->new( env => $env ); Dancer->dance($request); }; Unfortunately, the "ajax" route is not recognised and it gives me a 404 However I find that when I add Dancer::Handler->init_request_headers($env); in the sub, the Ajax route works fine. Dancer::Handler docs are not available and I need to understand why this works Would appreciate if someone could explain Thank you
How awesome of you to write it *right after* the weekend! :) I've closed a bunch of pull requests and issues. One of which... relates exactly to this! Unfortunately it's not in the latest version but it's already merged into devel branch and I intend to close another issue or two and then release (hopefully tonight, tomorrow, this week at least) another version which will include this. On Sun, Jan 29, 2012 at 5:18 PM, Gurunandan Bhat <gbhat@pobox.com> wrote:
I am trying to run multiple Dancer apps under Plack, One of my routes is an "ajax" route using Dancer::Plugin::Ajax
My $app inside the psgi file is defined as:
my $app = sub { local $ENV{DANCER_APPDIR} = 'MyAppDir'; setting appdir => 'MyAppDir'; load_app "MyApp"; Dancer::App->set_running_app('MyApp'); Dancer::Config->load; my $request = Dancer::Request->new( env => $env ); Dancer->dance($request); };
Unfortunately, the "ajax" route is not recognised and it gives me a 404
However I find that when I add
Dancer::Handler->init_request_headers($env); in the sub, the Ajax route works fine. Dancer::Handler docs are not available and I need to understand why this works
Would appreciate if someone could explain
Thank you _______________________________________________ Dancer-users mailing list Dancer-users@perldancer.org http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
Thank you. Sincerely appreciated. On Sun, Jan 29, 2012 at 8:51 PM, sawyer x <xsawyerx@gmail.com> wrote:
How awesome of you to write it right after the weekend! :)
I've closed a bunch of pull requests and issues. One of which... relates exactly to this!
Unfortunately it's not in the latest version but it's already merged into devel branch and I intend to close another issue or two and then release (hopefully tonight, tomorrow, this week at least) another version which will include this.
On Sun, Jan 29, 2012 at 5:18 PM, Gurunandan Bhat <gbhat@pobox.com> wrote:
I am trying to run multiple Dancer apps under Plack, One of my routes is an "ajax" route using Dancer::Plugin::Ajax
My $app inside the psgi file is defined as:
my $app = sub { local $ENV{DANCER_APPDIR} = 'MyAppDir'; setting appdir => 'MyAppDir'; load_app "MyApp"; Dancer::App->set_running_app('MyApp'); Dancer::Config->load; my $request = Dancer::Request->new( env => $env ); Dancer->dance($request); };
Unfortunately, the "ajax" route is not recognised and it gives me a 404
However I find that when I add
Dancer::Handler->init_request_headers($env); in the sub, the Ajax route works fine. Dancer::Handler docs are not available and I need to understand why this works
Would appreciate if someone could explain
Thank you _______________________________________________ Dancer-users mailing list Dancer-users@perldancer.org http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
_______________________________________________ Dancer-users mailing list Dancer-users@perldancer.org http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
Unfortunately, the "ajax" route is not recognised and it gives me a 404
However I find that when I add
Dancer::Handler->init_request_headers($env); in the sub, the Ajax route works fine. Dancer::Handler docs are not available and I need to understand why this works
Would appreciate if someone could explain
Routes configured with the Ajax Plugin's "ajax" helper will only match when the "X-Requested-With" HTTP Header is present and has the value "XMLHttpRequest" (i.e. it's an AJAX request). You can see this in the app's console log... the route appears to be found successfully but then is rejected because of the missing X-Requested-With HTTP Header. Another way to see the problem is to configure a 'before' hook sub which calls Data::Dumper::Dumper on Dancer's request() object - you'll see the HTTP Headers value is undef. By adding the init_request_headers() call you're fixing this problem, and the Headers are correctly parsed and made available to Dancer's request object, so your route is now accepted. I hope that makes sense, regards, oliver.
It does. Thanks. On Mon, Jan 30, 2012 at 3:43 AM, Oliver Gorwits <oliver@cpan.org> wrote:
Unfortunately, the "ajax" route is not recognised and it gives me a 404
However I find that when I add
Dancer::Handler->init_request_headers($env); in the sub, the Ajax route works fine. Dancer::Handler docs are not available and I need to understand why this works
Would appreciate if someone could explain
Routes configured with the Ajax Plugin's "ajax" helper will only match when the "X-Requested-With" HTTP Header is present and has the value "XMLHttpRequest" (i.e. it's an AJAX request).
You can see this in the app's console log... the route appears to be found successfully but then is rejected because of the missing X-Requested-With HTTP Header. Another way to see the problem is to configure a 'before' hook sub which calls Data::Dumper::Dumper on Dancer's request() object - you'll see the HTTP Headers value is undef.
By adding the init_request_headers() call you're fixing this problem, and the Headers are correctly parsed and made available to Dancer's request object, so your route is now accepted.
I hope that makes sense,
regards, oliver.
We solved it by manually checking in is_ajax if the unparsed raw env variable exists. This fix will be available in the next release. I think your solution is better: parsing the headers at the proper time. Would you be so kind as to provide a patch? I would really appreciate it! On Mon, Jan 30, 2012 at 12:13 AM, Oliver Gorwits <oliver@cpan.org> wrote:
Unfortunately, the "ajax" route is not recognised and it gives me a 404
However I find that when I add
Dancer::Handler->init_request_headers($env); in the sub, the Ajax route works fine. Dancer::Handler docs are not available and I need to understand why this works
Would appreciate if someone could explain
Routes configured with the Ajax Plugin's "ajax" helper will only match when the "X-Requested-With" HTTP Header is present and has the value "XMLHttpRequest" (i.e. it's an AJAX request).
You can see this in the app's console log... the route appears to be found successfully but then is rejected because of the missing X-Requested-With HTTP Header. Another way to see the problem is to configure a 'before' hook sub which calls Data::Dumper::Dumper on Dancer's request() object - you'll see the HTTP Headers value is undef.
By adding the init_request_headers() call you're fixing this problem, and the Headers are correctly parsed and made available to Dancer's request object, so your route is now accepted.
I hope that makes sense,
regards, oliver.
_______________________________________________ Dancer-users mailing list Dancer-users@perldancer.org http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
participants (3)
-
Gurunandan Bhat -
Oliver Gorwits -
sawyer x