-
-
Save Zettersten/2385580 to your computer and use it in GitHub Desktop.
Simplest cross-browser cross domain XHR ever.
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 request = function(url, success) { | |
if ('XDomainRequest' in window) { | |
var xdr = new XDomainRequest(); | |
xdr.onload = function() { | |
alert(xdr.responseText); | |
success(JSON.parse(xdr.responseText)); | |
}; | |
xdr.open('GET', url); | |
xdr.send(null); | |
} else { | |
var xhr = new XMLHttpRequest(); | |
xhr.onreadystatechange = function() { | |
if (xhr.readyState == 4) { | |
success(JSON.parse(xhr.responseText)); | |
} | |
}; | |
xhr.open('GET', url, true); | |
xhr.setRequestHeader('Accept', 'application/json'); | |
xhr.send(null); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment