Last active
March 9, 2016 17:48
-
-
Save gregglind/f8a2a3f9664ad1b59cbc to your computer and use it in GitHub Desktop.
failed 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
log = console.log.bind(console); | |
error = console.error.bind(console); | |
function cors1 (url, method, data) { | |
return new Promise(function (resolve, reject) { | |
var createCORSRequest = function(method, url) { | |
var xhr = new XMLHttpRequest(); | |
if ("withCredentials" in xhr) { | |
// Most browsers. | |
xhr.open(method, url, true); | |
} else if (typeof XDomainRequest != "undefined") { | |
// IE8 & IE9 | |
xhr = new XDomainRequest(); | |
xhr.open(method, url); | |
} else { | |
// CORS not supported. | |
xhr = null; | |
} | |
return xhr; | |
}; | |
console.log("creating", method, url) | |
var xhr = createCORSRequest(method, url); | |
// if you remove this line, you get a 415 error instead. | |
xhr.setRequestHeader("Content-Type", 'application/json'); | |
xhr.onload = function() { | |
// Success code goes here. | |
console.log("OK", xhr.status); | |
resolve("OK") | |
}; | |
xhr.onerror = function() { | |
// Error code goes here. | |
console.error("NOPE", xhr.statusText); | |
reject("nope"); | |
}; | |
console.log("data:", data); | |
// now break it... | |
xhr.send(JSON.stringify(data)); | |
}) | |
} | |
cors1("https://input.mozilla.org/api/v2/hb/","post",{}).then(log,error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment