Skip to content

Instantly share code, notes, and snippets.

@ddqd
Created May 14, 2014 21:35
Show Gist options
  • Select an option

  • Save ddqd/338f0dc04ffd5e23cd5f to your computer and use it in GitHub Desktop.

Select an option

Save ddqd/338f0dc04ffd5e23cd5f to your computer and use it in GitHub Desktop.
jsHttpReq
var CoreApi = function(){
var host = "host"
var post = function (path, params, callback) {
request(host, 'POST', params, callback)
}
var request = function (url, method, params, callback) {
var xhr = new XMLHttpRequest();
if(params == 'POST'){
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
}
xhr.onreadystatechange = (function(myxhr) {
return function() {
callback(myxhr);
}
})(xhr);
xhr.open(method, url, true);
xhr.send(params);
}
return {
get : function (path, callback) {
request(host+path, 'GET', '', callback)
},
post : function (path, params, callback) {
request(host, 'POST', params, callback)
}
}
}()
function register(callback) {
CoreApi.get("", callback)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment