Advertisement
Guest User

Untitled

a guest
May 30th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. use Mojolicious::Lite;
  2.  
  3. helper get_contents => sub {
  4. my $c = shift;
  5. if ($c->req->method eq 'GET' or $c->req->method eq 'HEAD') {
  6. return $c->req->url->query;
  7. } else {
  8. return $c->req->body;
  9. }
  10. };
  11.  
  12. any '/' => sub {
  13. my $c = shift;
  14. my $contents = $c->get_contents;
  15. my $input = $c->param('text');
  16. $c->stash(input => $input, output => $contents);
  17. }, 'index';
  18.  
  19. any '/test' => sub {
  20. my $c = shift;
  21. my $contents = $c->get_contents;
  22. $c->res->headers->content_type('text/plain');
  23. $c->render(text => $contents);
  24. };
  25.  
  26. app->start;
  27. __DATA__
  28.  
  29.  
  30. @@ index.html.ep
  31. <script src="//code.jquery.com/jquery-2.1.3.min.js"></script>
  32. <form id="test" action="/">
  33. <input id="text" name="text" value="<%= $input %>"></input>
  34. <input type="submit" value="GET jquery"/>
  35. <input type="submit" value="POST jquery"/>
  36. <input type="submit" value="GET form" formmethod="get"/>
  37. <input type="submit" value="POST form" formmethod="post"/>
  38. </form>
  39. <div id="output"><%= $output %></div>
  40. <script type="text/javascript">
  41. $("#test").submit(function(event) {
  42. var submitted = $("input[type=submit]:focus").val();
  43. var text = $("#text").val();
  44. if (submitted === "GET jquery") {
  45. $.get('/test', { 'text': text }, function(data) {
  46. $("#output").text(data);
  47. });
  48. return false;
  49. } else if (submitted === "POST jquery") {
  50. $.post('/test', { 'text': text }, function(data) {
  51. $("#output").text(data);
  52. });
  53. return false;
  54. }
  55. return true;
  56. });
  57. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement