0

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?

kwicz
  • 63
  • 7

1 Answers1

1

This '/{name}' is not correct string interpolation syntax. It is evaluated literally as /{name}. I would have done '/' + name, but curiosity got the better of me and found the accepted answer to this question shows the correct syntax.

DinoCoderSaurus
  • 6,110
  • 2
  • 10
  • 15
  • Thank you! Funny enough, after a day of trying to figure it out, I was just in the middle of trying to look up more information about that part of the code. Will try now! – kwicz Dec 28 '19 at 22:24