I'm trying to create a chat app using Flask and JavaScript, but I'm running into a problem. I used jQuery to accept the username and message, and Flask to put the message on the page. Is there anyway I can do this in real-time, and without reloading the page?
(to collect data and convert to JSON)
$(function () {
$('#send').bind('click', function () {
$.getJSON('/send', {
user: $('#username').val(),
mess: $('#message').val()
}, function (data) {
console.log('message sent')
//$("#result").append(data.result + '<br>');
});
return false;
});
});
(app.py)
messages = []
@app.route('/')
def index():
return render_template('index.html', messages=messages)
@app.route('/send',)
def send():
try:
username = request.args.get('user')
message = request.args.get('mess')
messages.append([username, message])
return jsonify(result=username + ': ' + message + '; ')
except Exception as e:
return e