Last active
December 16, 2015 09:58
-
-
Save bl4de/5416388 to your computer and use it in GitHub Desktop.
For Forebone library I'm currently working on something like simple wrapper for XMLHttpRequest object. This is first skeleton of this object:
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Client-server polygon</title> | |
</head> | |
<body> | |
<div id="response"></div> | |
<script type="text/javascript"> | |
/** | |
* Simple XMLHTTPRequest object wrapper | |
* | |
*/ | |
Ajax = { | |
url : null, | |
xhr : null, | |
initialize: function(_url) { | |
this.xhr = new XMLHttpRequest(); | |
this.url = _url; | |
}, | |
_get: function(_param, success, error) { | |
this.xhr.open('GET', this.url, false); | |
this.xhr.send(_param); | |
switch (this.xhr.status) { | |
case 200: { | |
success(this.xhr.responseText); | |
}; break; | |
default: { | |
}; break; | |
} | |
} | |
}; | |
var responseDiv = document.getElementById('response'); | |
Ajax.initialize('server/response.php'); | |
Ajax._get(null, | |
function(response) { | |
// success callback | |
responseDiv.innerHTML = response; | |
}, | |
function() { | |
// error callback | |
} | |
); | |
</script> | |
</body> | |
</html> |
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
// server/response.php | |
<?php | |
echo "test"; |
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
Ajax = { | |
url : null, | |
xhr : null, | |
async: false, | |
initialize: function(_url, _async) { | |
this.xhr = new XMLHttpRequest(); | |
this.url = _url; | |
if (_async) this.async = true; | |
}, | |
_get: function(_param, success, error) { | |
this.xhr.open('GET', this.url, this.async); | |
var that = this; | |
this.xhr.onreadystatechange = function(e) { | |
if (that.xhr.readyState == 4) { | |
if (that.xhr.status == 200) { | |
success(that.xhr.responseText); | |
} | |
} | |
}; | |
this.xhr.send(_param); | |
if ( !this.async ) { | |
switch (this.xhr.status) { | |
case 200: { | |
success(this.xhr.responseText); | |
}; break; | |
default: { | |
}; break; | |
} | |
} | |
console.log(this.async); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The second version (gistfile3.js) is updated of async true/false handling.