I'm working on my first XMLHttpRequest and am not having much luck. I think the error is somewhere on the flask side because at the request.onload line, the request readystate is 1, so it skips straight to request.send().
Javascript:
// Load user's last page
loadPage('chat');
window.onpopstate = e => {
const data = e.state;
document.title = data.title;
document.querySelector('#body').innerHTML = data.text;
};
function loadPage(name) {
const request = new XMLHttpRequest();
request.open('GET', '/{name}');
request.onload = () => {
const response = request.responseText;
document.querySelector('#body').innerHTML = response;
document.title = name;
};
request.send();
};
And on the Flask side:
import requests
from flask import Flask, jsonify, render_template, request, session
@app.route('/chat', methods=['GET'])
def chat():
channel = session['channel']
print('channel: ', channel)
return jsonify ({'channel': channel, 'username': session['username']})
Also of note: After the GET request is made, 'channel' isn't printing on the server side, which tells me the GET request is just not actually being made. Any idea on what's holding me up here?