Skip to content

Instantly share code, notes, and snippets.

@ww24
Created June 17, 2012 02:41
Show Gist options
  • Save ww24/2943223 to your computer and use it in GitHub Desktop.
Save ww24/2943223 to your computer and use it in GitHub Desktop.
Cross Browser XHR
// XHR
var ajax = function (method, url, data, callback) {
var xhr = /*@cc_on!@*/true ? new XMLHttpRequest() : new XDomainRequest();
xhr.timeout = 3000;
xhr.ontimeout = function () {
alert("timeout");
};
xhr.onerror = function () {
alert("error");
};
xhr.onload = function () {
callback(xhr.responseText);
};
xhr.open(method, url, false);
if (method === "POST")
xhr.setRequestHeader("Content-Type", "application\/x-www-form-urlencoded; charset=UTF-8");
xhr.send(data);
};
// Usage
ajax(method, url, data, function (res) {
console.log(res);
});
@ww24
Copy link
Author

ww24 commented Jun 17, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment