Created
May 14, 2014 21:35
-
-
Save ddqd/338f0dc04ffd5e23cd5f to your computer and use it in GitHub Desktop.
jsHttpReq
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
| 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