-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinyxhr.min.js
9 lines (8 loc) · 1.41 KB
/
tinyxhr.min.js
1
2
3
4
5
6
7
8
9
// tinyxhr by Shimon Doodkin - licanse: public doamin - https://gist.github.com/4706967
//
// tinyxhr("http://site.com/ajaxaction",function (err,data,xhr){ if (err) console.log("goterr ",err,'status='+xhr.status); console.log(data) });
// tinyxhr("http://site.com/ajaxaction",function (err,data,xhr){ if (err) console.log("goterr ",err,'status='+xhr.status); console.log(data) },'POST','value1=1&value2=2');
// tinyxhr("http://site.com/ajaxaction.json",function (err,data,xhr){ if (err) console.log("goterr ",err,'status='+xhr.status); console.log(data); console.log(JSON.parse(data)) },'POST',JSON.stringify({value:1}),'application/javascript');
// cb - a callback function like: function (err,data,XMLHttpRequestObject){ if (err) throw err; }
//
module.exports=function(url,cb,method,post,contenttype){var c=url,a=cb,i=method,f=post,b=contenttype;var d,h;try{h=new XMLHttpRequest()}catch(g){try{h=new ActiveXObject("Msxml2.XMLHTTP")}catch(g){if(console){console.log("tinyxhr: XMLHttpRequest not supported")}return null}}d=setTimeout(function(){h.abort();a(new Error("tinyxhr: aborted by a timeout"),"",h)},10000);h.onreadystatechange=function(){if(h.readyState!=4){return}clearTimeout(d);a(h.status!=200?new Error("tinyxhr: server response status is "+h.status):false,h.responseText,h)};h.open(i?i.toUpperCase():"GET",c,true);if(!f){h.send()}else{h.setRequestHeader("Content-type",b?b:"application/x-www-form-urlencoded");h.send(f)}};