Skip to content

Instantly share code, notes, and snippets.

@miry
Created April 23, 2013 20:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save miry/5447203 to your computer and use it in GitHub Desktop.
Save miry/5447203 to your computer and use it in GitHub Desktop.
This is a simple Sinatra application with sample of cross domain sharing resource.
<html>
<head>
<title>
Demo Cross Domain
</title>
<!-- Le styles -->
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
<style>
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
</style>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="brand" href="#">Project name</a>
<div class="nav-collapse collapse">
<ul class="nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container">
<h1>Demo Cross-Origin Resource Sharing</h1>
<p>A simple app to show how it works. It sends to http://lvh.me:4567 by default.</p>
<div class="row">
<span class="span3">
<input type="text" value="http://lvh.me:4567/status" id="cross_domain_url">
</span>
</div>
<div class="row">
<div class="span3">
<a href="#" onclick="sendGetReq()" class="btn btn-primary">SEND GET REQUEST</a>
</div>
<div class="span3">
<a href="#" onclick="sendPostReq()" class="btn btn-primary">SEND POST REQUEST</a>
</div>
</div>
</div> <!-- /container -->
</body>
<script src="http://code.jquery.com/jquery-2.0.0.min.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
<script>
function sendGetReq() {
$.get(getUrl(), function() {
alert('success');
});
}
function sendPostReq() {
$.post(getUrl(), function() {
alert('success');
});
}
function getUrl() {
return $("#cross_domain_url").val();
}
</script>
</html>
require 'sinatra'
set :public_folder, 'public'
post '/status' do
#XMLHttpRequest cannot load http://lvh.me:4567/status. Origin http://localhost:4567 is not allowed by Access-Control-Allow-Origin.
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Request-Method'] = '*'
'{"status" : "ok"}'
end
get '/status' do
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Request-Method'] = '*'
'{"status" : "ok"}'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment