La mia domanda è, qual è il modo migliore per fare sistema di commenti come StackOverflow, Voglio dire che invio richiesta dal mio browser e tutti vedranno questo commento (o in altri browser) senza aggiornare la pagina come alcuni Chiacchierare.Ajax post commento come StackOverflow
La mia soluzione era quella di utilizzare setInterval
, ma credo che ci deve essere un altro modo
$(document).ready(function() {
get();
$('#send').click(function() {
$.post('http://localhost/mvc.com/comment/post', {
n_id: parseInt(newsId),
user_id: $('#uid').val(),
text: $('#content').val(),
token: $('#token').val()
}, function (ret) {
if (ret.comm.err) {
$('.f').empty().prepend('<li id=e><h3 style="color: red">ERROR</h3></li>');
return false;
}
get();
});
setInterval(get,3000);
});
$('#content').keypress(function(e){
var key = e.which;
var cnt=$(this).val().length;
var max=100;
var tot=parseInt(max-cnt);
if(key >= 33 || key == 13 || key == 32) {
if (parseInt(tot) <= 0) {
e.preventDefault();
}
}
});
function get() {
$.post('http://localhost/mvc.com/comment', {get: parseInt(newsId)}, function (ret) {
$('.f').empty();
for (var key in ret.comm) {
$('.f').append('<li class=c id=' + ret.comm[key].id +
'><span>' + ret.comm[key].name + '</span><hr><br>' + ret.comm[key].text + '</li>');
}
});
}
[WebSockets] (https://developer.mozilla.org/en/docs/WebSockets) è una soluzione più bello di setInterval. –
L'aggiunta di 'setInterval' senza cancellare quella precedente su ogni' # send' sembra un'idea pericolosa. 'var tot = parseInt (max - cnt);' invece di semplice 'var tot = max - cnt;' sembra strano. 'parseInt (tot) <= 0' invece di' tot <= 0' è strano. – Regent
Se hai familiarità con le tecnologie .Net, provalo in SignalR ti offre buone soluzioni. –