Skip to content

Instantly share code, notes, and snippets.

@bl4de
Last active December 16, 2015 09:58
Show Gist options
  • Save bl4de/5416388 to your computer and use it in GitHub Desktop.
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:
<!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>
// server/response.php
<?php
echo "test";
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);
}
};
@bl4de
Copy link
Author

bl4de commented Apr 19, 2013

The second version (gistfile3.js) is updated of async true/false handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment