-
-
Save etsms/f1c6562bb2b9aa8fdb3970199718881f to your computer and use it in GitHub Desktop.
Simple Cross-browser XHR request with Cors support. Supported by any moder browser and IE>=8.
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
function ajax(type, url, data, callback) { | |
var xhr = new(this.XMLHttpRequest || ActiveXObject)('MSXML2.XMLHTTP.3.0'); | |
var req = []; | |
for(var item in data) { | |
req.push(encodeURIComponent(item) + "=" + encodeURIComponent(data[item])); | |
} | |
data = req.join("&"); | |
xhr.open(data ? 'POST' : 'GET', url, 1); | |
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); | |
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | |
xhr.open(type, url, true); | |
xhr.onload = function() { | |
if (this.status == 200) { | |
callback(JSON.parse(this.responseText), this); | |
return; | |
}; | |
}; | |
xhr.send(data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment