Skip to content

Instantly share code, notes, and snippets.

@ErosLever
ErosLever / uxhr.js
Last active April 19, 2017 21:37
uXHR (micro XMLHttpRequest wrapper) - Rigorously minified manually in 0xFF bytes
// License: public domain - original author: Eros Lever - https://gist.github.com/ErosLever/1c555eaca5d2bc07fc73bae7c550f1f5
// Inspired by tinyxhr.js (https://gist.github.com/4706967) and empijei (https://github.com/empijei)
uxhr=(u,c,d,h,p,m)=>(K=(z,f)=>z?Object.keys(z).map(f):0,e=encodeURIComponent,x=new XMLHttpRequest,x.open(m?m:d?'POST':'GET',u),K(h,k=>x.setRequestHeader(k,h[k])),K(p,k=>x[k]=p[k]),x.onload=_=>c(x),x.send(d?d.trim?d:K(d,k=>e(k)+'='+e(d[k])).join('&'):''));
/*
// Example usages:
uxhr("/logout",(x)=>alert(x.responseText))
uxhr("/login",(x)=>alert(x.responseText),{username:'admin',password:'admin'})
uxhr("/api",(x)=>alert(x.responseText),JSON.stringify({test:1234}),{'Content-Type':'application/json'})