[Kiev-pm] Редирект в Dancer2
Sergiy Borodych
sergiy.borodych at gmail.com
Thu Jan 2 23:16:02 PST 2014
Hello,
2013/12/18 TheAthlete <theathlet ÎÁ yandex.ru>
> Здравствуйте!
> Имеется вот такое автономное приложение на Dancer2:
>
> app.pl
>
> #!/usr/bin/env perl
> use Dancer2;
>
> use Data::Section::Simple qw/get_data_section/;
> use DBI;
> use utf8;
>
> my $index = get_data_section('index.html');
>
> # Settings
> #---------------------
> set charset => "UTF-8";
> set serializer => 'JSON';
> #---------------------
>
> get '/' => sub { 'hello' };
>
> any ['get', 'post'] => '/login' => sub {
> if (request->method() eq 'POST') {
> my $username = param 'username';
> my $password = param 'password';
>
> my $dbfile = 'mydb.db';
> my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","") or die
> $DBI::errstr;
>
> my $statement = qq{SELECT id FROM users WHERE username=? and
> password=?};
>
> my $sth = $dbh->prepare($statement) or die $dbh->errstr;
> my $rv = $sth->execute($username, $password) or die $sth->errstr;
>
> my ($userID) = $sth->fetchrow_array;
>
> if ($userID) {
> redirect '/';
> } else {
> if (request->is_ajax) {
> response->content_type('application/json');
> return {error => 'username or password is wrong'};
> }
> }
> }
>
> return $index;
> };
>
>
> dance;
>
> __DATA__
> @@ index.html
> <!DOCTYPE html>
> <html>
> <head>
> <meta charset="utf-8" />
> <title>Very simple login using Perl, Lovely.IO, Ajax, JSON and
> SQLite</title>
> <link rel="stylesheet" type="text/css" media="screen, projection"
> href="http://www.blueprintcss.org/blueprint/screen.css" />
> <link rel="stylesheet" type="text/css" media="screen, projection"
> href="http://www.blueprintcss.org/blueprint/plugins/buttons/screen.css" />
> <link rel="stylesheet" type="text/css" media="print" href="
> http://www.blueprintcss.org/blueprint/print.css" />
>
> <!--[if IE]><link rel="stylesheet" type="text/css" media="screen,
> projection" href="http://www.blueprintcss.org/blueprint/ie.css"><![
> endif]-->
> <script type="text/javascript" src="http://code.jquery.com/
> jquery-2.0.3.min.js"></script>
>
> <script type="text/javascript">
> $(document).ready(function(){
> $("#loginForm").submit(function() { // loginForm отправлена
>
> $.post('/login', { "username": $('#username').val(), "password":
> $('#password').val() }, function(data, textStatus, jqXHR){
> if (data.error) { // сценарий возвратил ошибку
> $('#loginResult').text("data.error: " +
> data.error).addClass("error");
> }
> });
>
> $('#loginResult').fadeIn();
> return false;
> });
> });
> </script>
>
> <style type="text/css">
> #loginContent { width: 350px; margin: 100px auto; }
> button[type] { margin: 0.5em 0; }
> dd {margin: 0; padding: 0; }
> </style>
> </head>
> <body>
> <div id="loginContent" class="container">
> <div id="loginResult" style="display:none;"></div>
> <form id="loginForm" name="loginForm" method="post" action="/login">
> <fieldset>
> <legend>Enter information</legend>
>
> <dl>
> <dt><label for="username">Username</label></dt>
> <dd><input type="text" id="username" name="username"
> class="text" size="20" /></dd>
> <dt><label for="password">Password</label></dt>
> <dd><input type="password" id="password" name="password"
> class="text" size="20" /></dd>
> </dl>
>
> <button type="submit" class="button positive"><img alt="ok" src="
> http://www.blueprintcss.org/blueprint/plugins/buttons/icons/tick.png" />
> Login</button>
> </fieldset>
> </form>
> </div>
> </body>
> </html>
>
> Запускаю с помощью
> $ plackup app.pl
>
> Подскажите пожайлуста, как сделать редирект на роут '/', чтобы на выходе
> появилось 'hello':
> get '/' => sub { 'hello' };
>
> Сейчас никакого редиректа не происходит, хотя в лог пишется, что он прошел:
>
Не совсем понятно как проверяешь. По логам - да есть редирект.
Покажи
wget -O - http://localhost:3000/
> 127.0.0.1 - - [18/Dec/2013:14:01:41 +0200] "GET /login HTTP/1.1" 200 4694
> "-" "Opera/9.80 (X11; Linux x86_64) Presto/2.12.388 Version/12.16"
> 127.0.0.1 - - [18/Dec/2013:14:01:49 +0200] "POST /login HTTP/1.1" 302 0 "
> http://localhost:5000/login" "Opera/9.80 (X11; Linux x86_64)
> Presto/2.12.388 Version/12.16"
> 127.0.0.1 - - [18/Dec/2013:14:01:49 +0200] "GET / HTTP/1.1" 200 5 "
> http://localhost:5000/login" "Opera/9.80 (X11; Linux x86_64)
> Presto/2.12.388 Version/12.16"
>
> Подскажите, в чем может быть проблема?
>
Посмотри (кажется твой случай)
https://en.wikipedia.org/wiki/Post/Redirect/Get
--
Sergiy Borodych
----------- ���� ���-----------
÷ÌÏÖÅÎÉÅ × ÆÏÒÍÁÔÅ HTML ÂÙÌÏ ÉÚ×ÌÅÞÅÎÏ…
URL: <http://mail.pm.org/pipermail/kiev-pm/attachments/20140103/592f0983/attachment.html>
More information about the Kiev-pm
mailing list