Created
June 3, 2014 19:58
-
-
Save datchley/c105ba5408e30fbcee10 to your computer and use it in GitHub Desktop.
Plain javascript XMLHttpRequest implementation.
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 xhr(){ | |
| try { | |
| return new XMLHttpRequest(); | |
| }catch(e){} | |
| try { | |
| return new ActiveXObject("Msxml3.XMLHTTP"); | |
| }catch(e){} | |
| try { | |
| return new ActiveXObject("Msxml2.XMLHTTP.6.0"); | |
| }catch(e){} | |
| try { | |
| return new ActiveXObject("Msxml2.XMLHTTP.3.0"); | |
| }catch(e){} | |
| try { | |
| return new ActiveXObject("Msxml2.XMLHTTP"); | |
| }catch(e){} | |
| try { | |
| return new ActiveXObject("Microsoft.XMLHTTP"); | |
| }catch(e){} | |
| return null; | |
| } | |
| function ajaxRequest(url, callback, postData) { | |
| var req = xhr(); | |
| if (!req) return; | |
| var method = (postData) ? "POST" : "GET"; | |
| req.open(method, url, true); | |
| // req.setRequestHeader('User-Agent','XMLHTTP/1.0'); | |
| if (postData) | |
| req.setRequestHeader('Content-type','application/x-www-form-urlencoded'); | |
| req.onreadystatechange = function () { | |
| if (req.readyState != 4) return; | |
| if (req.status != 200 && req.status != 304) { | |
| return; | |
| } | |
| callback(req); | |
| } | |
| if (req.readyState == 4) return; | |
| req.send(postData); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment