Skip to content

Instantly share code, notes, and snippets.

@fillano
Created August 29, 2011 06:07
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 fillano/1177858 to your computer and use it in GitHub Desktop.
Save fillano/1177858 to your computer and use it in GitHub Desktop.
CORS test
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin': 'http://localhost'
});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/ with CORS.');
<html>
<body>
<input type="button" value="test" id="test">
</body>
</html>
<script>
(function(window, undefined){
var document = window.document,
btn1 = document.getElementById('test');
btn1.onclick = function(e) {
var req = getRequestObject();
if(typeof req !== 'null') {
req.open('GET', 'http://localhost:1337', true);
req.onreadystatechange = function() {
if(this.readyState === this.DONE) {
if(this.status === 200) {
log(this.responseText);
log(this.responseXML);
} else {
log(this.status);
}
} else {
log(this.readyState);
}
};
req.send(null);
} else {
log('XMLHttpRequest unavailable.');
}
};
function getRequestObject() {
try {
return new XMLHttpRequest();
}catch(e){return null;}
}
function log(m) {
if(window.console) window.console.log(m);
else alert(m);
}
})(this);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment