Created
August 27, 2015 14:10
-
-
Save mrsimonemms/92b185c9f56736a166e7 to your computer and use it in GitHub Desktop.
CORS
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://code.jquery.com/jquery-2.1.4.min.js" data-semver="2.1.4" data-require="[email protected]"></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> |
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
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