diff options
author | Alexander Tkachev | 2016-07-19 12:15:51 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | 09ae2f7593a66817f86fdb4f65076b24d9b2ff40 (patch) | |
tree | d258ad9e6dd45262dd548cf76cb0d6e236c2d187 /backends/networking/wwwroot/ajax.js | |
parent | 5163fb4e02926bcc9fe12de3f3b031b1c8349c8b (diff) | |
download | scummvm-rg350-09ae2f7593a66817f86fdb4f65076b24d9b2ff40.tar.gz scummvm-rg350-09ae2f7593a66817f86fdb4f65076b24d9b2ff40.tar.bz2 scummvm-rg350-09ae2f7593a66817f86fdb4f65076b24d9b2ff40.zip |
CLOUD: Add "/filesAJAX" sketch
It works already, but still requires some polishing.
Diffstat (limited to 'backends/networking/wwwroot/ajax.js')
-rw-r--r-- | backends/networking/wwwroot/ajax.js | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/backends/networking/wwwroot/ajax.js b/backends/networking/wwwroot/ajax.js new file mode 100644 index 0000000000..12c50f1e72 --- /dev/null +++ b/backends/networking/wwwroot/ajax.js @@ -0,0 +1,48 @@ +// the following is snippet from http://stackoverflow.com/a/18078705 +// I changed a few things though + +var ajax = {}; +ajax.x = function () { return new XMLHttpRequest(); }; // "no one uses IE6" + +ajax.send = function (url, callback, errorCallback, method, data, async) { + if (async === undefined) async = true; + + var x = ajax.x(); + x.open(method, url, async); + x.onreadystatechange = function () { + if (x.readyState == XMLHttpRequest.DONE) { + if (x.status == 200) + callback(x.responseText); + else + errorCallback(x); + } + }; + if (method == 'POST') { + x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); + } + x.send(data) +}; + +ajax.get = function (url, data, callback, errorCallback, async) { + var query = []; + for (var key in data) { + query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key])); + } + ajax.send(url + (query.length ? '?' + query.join('&') : ''), callback, errorCallback, 'GET', null, async) +}; + +ajax.post = function (url, data, callback, errorCallback, async) { + var query = []; + for (var key in data) { + query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key])); + } + ajax.send(url, callback, errorCallback, 'POST', query.join('&'), async) +}; + +ajax.getAndParseJson = function (url, data, callback) { + ajax.get( + url, data, + function (responseText) { callback(JSON.parse(responseText)); }, + function (x) { console.log("error: " + x.status); } + ); +};
\ No newline at end of file |