Submitting comment. Work in progress
This commit is contained in:
parent
504e0347bf
commit
a200fdfa88
3 changed files with 81 additions and 5 deletions
|
@ -117,7 +117,6 @@ instance d'ici peu.</p>
|
||||||
<p><em>(Les champs obligatoires sont notés avec une *)</em></p>
|
<p><em>(Les champs obligatoires sont notés avec une *)</em></p>
|
||||||
<div id="onestpotes" style="display:none">
|
<div id="onestpotes" style="display:none">
|
||||||
<input id="captcha" name="captcha" placeholder="Ne pas remplir si vous êtes humain" type="text">
|
<input id="captcha" name="captcha" placeholder="Ne pas remplir si vous êtes humain" type="text">
|
||||||
<input name="article" id="article" value="972b3e0144e82a9496a2ab9a275e7368" type="hidden">
|
|
||||||
</div>
|
</div>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
<button type="button" onclick="preview_markdown(); return false;" class="button-warning pure-button">Prévisualiser</button>
|
<button type="button" onclick="preview_markdown(); return false;" class="button-warning pure-button">Prévisualiser</button>
|
||||||
|
|
|
@ -48,9 +48,16 @@ function comments_loaded(response) {
|
||||||
|
|
||||||
function new_comment() {
|
function new_comment() {
|
||||||
var author = document.getElementById('author').value;
|
var author = document.getElementById('author').value;
|
||||||
// TODO make CORS POST request
|
var email = document.getElementById('email').value;
|
||||||
// and asynchronously redirect depending on result
|
var site = document.getElementById('site').value;
|
||||||
console.log('SUBMIT ' + author);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
|
|
@ -1,5 +1,48 @@
|
||||||
// Copyright (c) 2015 Yannic ARNOUX
|
// 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.
|
// Create the XHR object.
|
||||||
function stacosys_get_cors_request(method, url) {
|
function stacosys_get_cors_request(method, url) {
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
|
@ -58,7 +101,34 @@ function stacosys_load(callback) {
|
||||||
};
|
};
|
||||||
|
|
||||||
xhr.onerror = function() {
|
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();
|
xhr.send();
|
||||||
|
|
Loading…
Add table
Reference in a new issue