#!/usr/bin/perl -- # Automatically use-s strict/warnings/utf8 and feature :5.10 use Mojolicious::Lite; sub mojo_index { my( $self ) = @_; my $time = time; return $self->render( title => "Mojo $time"); } ## delay ajax requests (localhost too fast for disable visualfx) sub sleeper { my( $self ) = @_; if( $self->req->headers->header('x-requested-with') ) { ## "XMLHttpRequest" sleep 1.3; } return; } ## map paths to subroutines ### #~ route named 'index', render's template index.html.ep if html is accepted get '/' => \&mojo_index => 'index';;; #~ https://localhost:3000/index #~ https://localhost:3000/index.html #~ https://localhost:3000/index.txt get '/index' => sub { shift->render( title => "Mojo \$time") }; #~ https://localhost:3000/error post '/error' => sub { sleeper(@_); shift->render( data => 'this is not json (duh)' ); }; #~ https://localhost:3000/simple any [qw[ GET POST ]] => '/simple' => sub { sleeper(@_); my( $self ) = @_; my $number = $self->param('toy_number') ; my $not_a = scalar( Scalar::Util::looks_like_number( $number ) ) ? 'looks_like_number' : 'DOES NOT looks_like_number' ;;;;;; my $message = $self->render( partial => 1, ## no http number => $number, not_a => $not_a, inline => q{You chose the (<%= $number =%>) and it is <%= $not_a =%> }, ); return $self->render( json => { result => $message } ); }; # Start the Mojolicious command system _aka_ shagadelic('daemon'); app->start; __DATA__ @@ index.txt.ep If you ask for text you will get text its <%= title %> @@ index.html.ep
%= form_for $action => (method => $method) => begin %= hidden_field toy_number => 42 %= submit_button "$method /$action" %= end | <% } ## end $action =%> <% } ## end $method =%>
" +
JSON.stringify(
{uhoh:1, message: msg,exception:e},
null,2
)
);
}
};
var toy_ajax = {
type: 'POST',
url: route,
data: { 'toy_number': $('#toy_number').val() },
dataType: 'json', /* i want this */
accepts: { 'json': 'application/json' }, /* send Accept: this */
};
/*** do that thing, remoting ***/
$('#toy_toy_toy *').attr('disabled', 'disabled' );
$('#toy_result').html('.ajax-ing the ' + route );
$.ajax( toy_ajax ).always( toy_always ).done( toy_done ).fail( toy_fail );
};
####
perl moquery.pl daemon
perl moquery.pl daemon --mode=production -l http://localhost:80