From a200fdfa88737b6e759cdeabe3fea8503376766b Mon Sep 17 00:00:00 2001 From: Yax <1949284+kianby@users.noreply.github.com> Date: Sun, 10 May 2015 19:39:32 +0200 Subject: [PATCH] Submitting comment. Work in progress --- demo/public/index.html | 1 - demo/public/js/page.js | 13 +++++-- demo/public/js/stacosys.js | 72 +++++++++++++++++++++++++++++++++++++- 3 files changed, 81 insertions(+), 5 deletions(-) diff --git a/demo/public/index.html b/demo/public/index.html index 3abe8ae..41c4db0 100644 --- a/demo/public/index.html +++ b/demo/public/index.html @@ -117,7 +117,6 @@ instance d'ici peu.

(Les champs obligatoires sont notés avec une *)

diff --git a/demo/public/js/page.js b/demo/public/js/page.js index a1b176e..d027626 100644 --- a/demo/public/js/page.js +++ b/demo/public/js/page.js @@ -48,9 +48,16 @@ function comments_loaded(response) { function new_comment() { var author = document.getElementById('author').value; - // TODO make CORS POST request - // and asynchronously redirect depending on result - console.log('SUBMIT ' + author); + var email = document.getElementById('email').value; + var site = document.getElementById('site').value; + var captcha = document.getElementById('captcha').value; + //var subscribe = document.getElementById('subscribe').value; + + stacosys_new(author, email, site, captcha, comment_submitted); +} + +function comment_submitted(success) { + console.log('SUBMITTED : ' + success); } // -------------------------------------------------------------------------- diff --git a/demo/public/js/stacosys.js b/demo/public/js/stacosys.js index d8cd38b..ef1405c 100644 --- a/demo/public/js/stacosys.js +++ b/demo/public/js/stacosys.js @@ -1,5 +1,48 @@ // Copyright (c) 2015 Yannic ARNOUX +/** + * 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 callback {Function} to callback on completion + * @param errback {Function} to callback on error + */ +function xdr(url, method, data, callback, errback) { + var req; + + 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')); + } + } + }; + req.send(data); + } + } else if(XDomainRequest) { + req = new XDomainRequest(); + req.open(method, url); + req.onerror = errback; + req.onload = function() { + callback(req.responseText); + }; + req.send(data); + } else { + errback(new Error('CORS not supported')); + } +} + + // Create the XHR object. function stacosys_get_cors_request(method, url) { var xhr = new XMLHttpRequest(); @@ -58,7 +101,34 @@ function stacosys_load(callback) { }; xhr.onerror = function() { - alert('Woops, there was an error making the request.'); + console.log('Woops, there was an error making the request.'); + }; + + xhr.send(); +} + +function stacosys_new(author, email, site, captcha, callback) { + + var url = STACOSYS_URL + '/comments?token=' + STACOSYS_TOKEN + + '&url=' + STACOSYS_PAGE + '&author=' + author + + '&email=' + email + '&site=' + site + + '&captcha=' + captcha; + var xhr = stacosys_get_cors_request('POST', url); + if (!xhr) { + console.log('CORS not supported'); + callback(false); + return; + } + + // Response handlers. + xhr.onload = function() { + var jsonResponse = JSON.parse(xhr.responseText); + callback(jsonResponse); + }; + + xhr.onerror = function() { + console.log('Woops, there was an error making the request.'); + callback(false); }; xhr.send();