Created
December 10, 2015 16:17
-
-
Save Wallsays/ecea0528c40d8d252e13 to your computer and use it in GitHub Desktop.
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
# Create the XHR object. | |
createCORSRequest = (method, url) -> | |
xhr = new XMLHttpRequest | |
if 'withCredentials' of xhr | |
# XHR for Chrome/Firefox/Opera/Safari. | |
xhr.open method, url, true | |
else if typeof XDomainRequest != 'undefined' | |
# XDomainRequest for IE. | |
xhr = new XDomainRequest | |
xhr.open method, url | |
else | |
# CORS not supported. | |
xhr = null | |
xhr | |
# Helper method to parse the title tag from the response. | |
getTitle = (text) -> | |
text.match('<title>(.*)?</title>')[1] | |
# Make the actual CORS request. | |
makeCorsRequest = -> | |
# All HTML5 Rocks properties support CORS. | |
url = 'http://updates.html5rocks.com' | |
xhr = createCORSRequest('GET', url) | |
if !xhr | |
alert 'CORS not supported' | |
return | |
# Response handlers. | |
xhr.onload = -> | |
text = xhr.responseText | |
title = getTitle(text) | |
alert 'Response from CORS request to ' + url + ': ' + title | |
return | |
xhr.onerror = -> | |
alert 'Woops, there was an error making the request.' | |
return | |
xhr.send() | |
return | |
url = 'http://localhost:3000/api/v1/qualifications' | |
xhr = createCORSRequest('options', url) | |
console.log( xhr.send() ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment