Created
August 29, 2011 06:07
-
-
Save fillano/1177858 to your computer and use it in GitHub Desktop.
CORS test
This file contains hidden or 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 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.'); |
This file contains hidden or 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
<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