Files
stacosys/demo/public/js/stacosys.js
T

78 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-05-10 19:39:32 +02:00
/**
* Make a X-Domain request to url and callback.
*
* @param url {String}
* @param method {String} HTTP verb ('GET', 'POST', 'DELETE', etc.)
* @param data {String} request body
* @param header {Dict} header options
2015-05-10 19:39:32 +02:00
* @param callback {Function} to callback on completion
* @param errback {Function} to callback on error
*/
function xdr(url, method, data, header, callback, errback) {
2015-05-10 19:39:32 +02:00
var req;
2015-05-10 19:39:32 +02:00
if(XMLHttpRequest) {
req = new XMLHttpRequest();
if('withCredentials' in req) {
req.open(method, url, true);
req.onerror = errback;
req.onreadystatechange = function() {
if (req.readyState === 4) {
if (req.status >= 200 && req.status < 400) {
callback(req.responseText);
} else {
errback(new Error('Response returned with non-OK status'));
}
}
};
for ( var h in header ) {
req.setRequestHeader(h, header[h]);
}
2015-05-10 19:39:32 +02:00
req.send(data);
}
} else if(XDomainRequest) {
req = new XDomainRequest();
req.open(method, url);
req.onerror = errback;
req.onload = function() {
callback(req.responseText);
};
for ( var h in header ) {
req.setRequestHeader(h, header[h]);
}
2015-05-10 19:39:32 +02:00
req.send(data);
} else {
errback(new Error('CORS not supported'));
}
}
function stacosys_get_count(callback, errback) {
2015-05-09 20:25:35 +02:00
var url = STACOSYS_URL + '/comments/count?token=' + STACOSYS_TOKEN + '&url=' + STACOSYS_PAGE;
xdr(url, 'GET', null, {}, callback, errback);
2015-05-09 15:16:56 +02:00
}
function stacosys_load_comments(callback, errback) {
var url = STACOSYS_URL + '/comments?token=' + STACOSYS_TOKEN + '&url=' + STACOSYS_PAGE;
xdr(url, 'GET', null, {}, callback, errback);
2015-05-10 19:39:32 +02:00
}
function stacosys_new_comment(author, email, site, captcha, subscribe, message, callback, errback) {
var url = STACOSYS_URL + '/comments';
var data = {
'token': STACOSYS_TOKEN,
'url': STACOSYS_PAGE,
'author': author,
'email': email,
'site': site,
'captcha': captcha,
'subscribe': subscribe,
'message': message
2015-05-10 19:39:32 +02:00
};
var header = {
'Content-type': 'application/json'
2015-05-09 12:51:14 +02:00
};
var j = JSON.stringify(data);
xdr(url, 'POST', JSON.stringify(data), header, callback, errback);
2015-05-09 12:51:14 +02:00
}