Skip to content

Instantly share code, notes, and snippets.

@mrsimonemms
Created August 27, 2015 14:10
Show Gist options
  • Save mrsimonemms/92b185c9f56736a166e7 to your computer and use it in GitHub Desktop.
Save mrsimonemms/92b185c9f56736a166e7 to your computer and use it in GitHub Desktop.
CORS
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.4.min.js" data-semver="2.1.4" data-require="jquery@2.1.4"></script>
</head>
<script>
$(function () {
$("#click").click(function () {
$.ajax({
data: JSON.stringify({
hello: "world"
}),
headers: {
Authorization: "Basic dXNlcjpwYXNz"
},
type: "POST",
url: "http://localhost:8080/hello",
success: function () {
console.log("yay");
},
error: function () {
console.log(arguments);
}
});
});
});
</script>
<body>
<a href="#" id="click">Click me</a>
</body>
</html>
var restify = require('restify');
function respond(req, res, next) {
res.send('hello ' + req.params.name);
next();
}
var server = restify.createServer();
server.use(function (req, res, next) {
console.log("corsed");
res.header("Access-Control-Allow-Origin", "http://localhost:63342"); /* Will need to change to your server */
res.header("Access-Control-Allow-Methods", "GET");
res.header("Access-Control-Allow-Headers", "Authorization");
return next();
});
server.get('/hello/:name', [
function (req, res, cb) {
console.log(req.headers.authorization);
cb();
}
], respond);
server.post('/hello', [
function (req, res, cb) {
console.log(req.headers.authorization);
cb();
}
], respond);
server.listen(8080, function() {
console.log('%s listening at %s', server.name, server.url);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment