Skip to content

Instantly share code, notes, and snippets.

@user19
Created March 6, 2015 03:25
Show Gist options
  • Save user19/75378d063d7666bde523 to your computer and use it in GitHub Desktop.
Save user19/75378d063d7666bde523 to your computer and use it in GitHub Desktop.
function xhr(url, payload, method, headers){
var x = new XMLHttpRequest();
x.open(method, url, false);
setRequestHeader(x, method, headers)
x.send(payload);
return x;
}
function axhr(url, payload, method, headers, responseType, callback){
var x = new XMLHttpRequest();
x.onload = function(){callback(x);}
x.open(method, url);
setRequestHeader(x, method, headers)
x.responseType = responseType;
x.send(payload);
}
function setRequestHeader(obj, method, headers){
if(method === 'POST')
obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
if(headers)
for(name in headers)
obj.setRequestHeader(name, headers[name]);
}
var xmlhttprequest = "\nxhr(url='', payload=null, method='GET', headers={});\naxhr(url='', payload=null, method='GET', headers={}, responseType='', callback);";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment