Skip to content

Instantly share code, notes, and snippets.

@zoffixznet
Last active January 10, 2017 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoffixznet/1b34ed99dbea341c19514feda5e47f75 to your computer and use it in GitHub Desktop.
Save zoffixznet/1b34ed99dbea341c19514feda5e47f75 to your computer and use it in GitHub Desktop.
Perl 6 AJAX demo
#!/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