Skip to content

Instantly share code, notes, and snippets.

@christierney
Created August 7, 2012 23:45
Show Gist options
  • Save christierney/3290605 to your computer and use it in GitHub Desktop.
Save christierney/3290605 to your computer and use it in GitHub Desktop.
testing basic auth ajax requests
#!/usr/bin/env ruby
# Launch, visit http://localhost:4567, authenticate with admin/secret, and wait.
require 'sinatra'
use Rack::Auth::Basic do |username, password|
username == 'admin' && password == 'secret'
end
get '/' do
'<!doctype html>
<meta charset="utf-8">
<title>auth test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
function getdata() {
console.log("getting data");
$.get("/data", function(data) {
console.log("got data: " + data);
$("ul").append("<li>"+data+"</li>");
});
}
$(function () {
setInterval(getdata, 3000);
});
</script>
<p>hello world.</p>
<ul>
</ul>
'
end
get '/data' do
rand.to_s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment