Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-23 08:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found