So i fill textarea, press submit and... i get raw json response in my browser. Question is: How do i separate "api" from "frontend" and made them pass data between each other in order to - have nice form

Note you can seprate your API from the frontend purely on the Perl level. Mojolicious includes content negotiation, as I show in the following example (run it e.g. via morbo script.pl). Note how POST and GET just work, including http://127.0.0.1:3000/myform?foo=helloworld&format=json and http://127.0.0.1:3000/myform.txt?foo=blahblah.

#!/usr/bin/env perl use Mojolicious::Lite -signatures; any '/' => sub ($c) { $c->redirect_to('/myform.html') }; sub my_api_do_thing ($arg) { # could/should be in a module! return $arg =~ s/[aeiou]//gr; } any '/myform' => sub ($c) { if ( defined $c->param('foo') ) { # if the form was submitted $c->stash( returnval => my_api_do_thing( $c->param('foo') ) ); } $c->respond_to( json => { json => { retval => $c->stash('returnval') } }, txt => { text => $c->stash('returnval') }, html => { template => 'myform' }, ); }; app->start; __DATA__ @@ myform.html.ep % layout 'main', title => 'Hello, World!'; <div> % if ( stash 'returnval' ) { <div> Return value: <%= stash 'returnval' %> </div> % } %= form_for myform => ( method=>'post' ) => begin <div> %= label_for foo => 'Foo' %= text_field foo=>'Foobar?' </div> <div> %= radio_button format => 'html', id=>'format_html', checked => un +def %= label_for format_html => 'HTML' %= radio_button format => 'json', id=>'format_json' %= label_for format_json => 'JSON' </div> <div> %= submit_button </div> %= end </div> @@ layouts/main.html.ep <!DOCTYPE html> <html> <head><title><%= title %></title></head> <body> %= content </body> </html>

In reply to Re: a simple web front end by haukex
in thread a simple web front end by lis128

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.