Tuesday, 2 August 2022

GET request is Sent Twice; Once With the Query String Inside the URL and Once Without It

My issue is that whenever the button const loadOldPlayer is clicked, it sends two GET requests, as shown in the screenshot below. What this results in is the wrong template being rendered (showsPlayer.html should be what is rendered but instead it just renders playerView.html). I can't figure out why this is happening, so any help is appreciated. Below the screenshot is my code. screenshot of issue

let playerName
const loadOldPlayer = document.getElementById('playerLoader');

const enterProfile = (usedToLoad) => {
    console.log(playerName)
    if (usedToLoad) {
        playerName = document.getElementById('loadPlayerName').value
    };
    const playerData = {
        playerName: playerName
    };
    const JSONdata = JSON.stringify(playerData);
    fetch(`/profile?tags=${JSONdata}`, { method: "GET" }).then((response) => {
        if (response.ok) {
            document.getElementById('loaderLabel').innerHTML = "Loading Player"
        }
        else {
            alert("Something bad happened.");
        };
    });
};

loadOldPlayer.addEventListener("click", enterProfile.bind(true));
from flask import Flask, render_template, request
from static.SNEKfiles import SpaceShipGame
import json


game_app = Flask(__name__)

@game_app.route('/') 
@game_app.route('/home')
def home():
    return render_template("HTMLPage1.html")

@game_app.route('/profile', methods=['GET'])
def profile():
    if request.method == 'GET':
        playerName = request.args.get('tags')
        if playerName != None:
            print("got the name")
            return render_template("showsPlayer.html")
        else:
            print("here is issue")
            return render_template("playerView.html")

if __name__== "__main__":
    game_app.run(debug=True, host='127.0.0.1', port=7777)

Yes my HTML files are badly named, I'll probably get around to fixing that. Eventually.



from GET request is Sent Twice; Once With the Query String Inside the URL and Once Without It

No comments:

Post a Comment