Last active
January 10, 2017 18:08
-
-
Save zoffixznet/1b34ed99dbea341c19514feda5e47f75 to your computer and use it in GitHub Desktop.
Perl 6 AJAX demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl6 | |
use HTTP::Server::Tiny; | |
use URI::Encode; | |
HTTP::Server::Tiny.new(:host<0.0.0.0>, :port(8888)).run: sub ($env) { | |
given $env<p6sgi.input>.slurp-rest { | |
my %q = $_ ?? .split(/<[=&]>/).map(*.&uri_decode).Hash !! (); | |
if %q { | |
say "Got: %q<>"; | |
return 200, ['Content-Type' => 'text/plain'], [ | |
"Got name: `%q<name>` and age: `%q<age>`" | |
]; | |
} | |
} | |
200, ['Content-Type' => 'text/html'], [ | |
q:to/HTML_END/ | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Perl 6 AJAX Demo</title> | |
</head> | |
<body> | |
<form action="/" method=POST id="stuff"> | |
<div><label>Your name: <input name="name"></label></div> | |
<div><label>Your age: <input name="age"></label></div> | |
<input type="submit" value="Send stuff"> | |
</form> | |
<script | |
src="https://code.jquery.com/jquery-3.1.1.min.js" | |
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" | |
crossorigin="anonymous"></script> | |
<script> | |
$(function(){ | |
$('#stuff').submit(function(e){ | |
$.ajax({ | |
type: $(this).attr('method'), | |
url: $(this).attr('action'), | |
data: $(this).serialize(), | |
success: function(data) { | |
alert("Response: " + data); | |
} | |
}); | |
e.preventDefault(); | |
}) | |
}); | |
</script> | |
</body> | |
</html> | |
HTML_END | |
] | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment