How do I send a fetch request with two parameters to my flask server? I have the flask working but I am not sure about the javascript part.
Javascript Code:
var symbol = document.getElementById("stock-symbol").value;
var quantity = document.getElementById("quantity").value;
fetch(`/game/market-simulator/make-sell-request/${symbol}${quantity}`)
Python Code:
@app.route("/game/market-simulator/make-sell-request/<symbol>/<quantity>", methods=["GET", "POST"])
def sell_stocks(symbol, quantity):
if request.method == "GET":
try:
symbol = str(symbol)
quantity = int(quantity)
except ValueError:
return render_template("sell-page.html")
return symbol, quantity
Using params like this:
javascript:
fetch(`/game/market-simulator/make-sell-request/?symbol=${symbol}&quantity=${quantity}`)
python:
from flask.globals import request
...
@app.route("/game/market-simulator/make-sell-request")
def sell_stocks():
symbol = request.args.get('symbol')
quantity= request.args.get('quantity')
return symbol, quantity
attention, params default type is string.
If you want to continue to use this flask routings,you can do this in JavaScript
fetch(`/game/market-simulator/make-sell-request/${symbol}/${quantity}`)
Pay attention to the sequence between symbol and quantity