Created
May 28, 2014 11:38
-
-
Save elrumordelaluz/92eeaa3fe98c2d8899b9 to your computer and use it in GitHub Desktop.
(js) httpRequest with callback
This file contains 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
var $ = { | |
core:function(u){ | |
return new $.httpRequest(u); | |
}, | |
httpRequest:function(url, callback){ | |
var r = new XMLHttpRequest(); | |
r.open("GET", url, true); | |
r.onreadystatechange = function () { | |
if (r.readyState != 4 || r.status != 200) return; | |
callback(r.responseText); | |
}; | |
r.send(); | |
} | |
}; | |
// Use: | |
$.httpRequest("http://examplesite.com/myurl", function(data) { | |
// callback | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment