This is my function in flask which is rolling 6 side dice:
@bp.route('/character', methods=('GET', 'POST'))
@login_required
def character():
throws = []
def roll_1d6():
return random.randint(1,6)
def roll_4d6(num_dice):
for i in range(num_dice):
throws.append(roll_1d6())
return json.dumps(throws)
return render_template('dd_roller/create_character.html', throws=throws)
I was also using make_response(json.dumps(throws)) and jsonify(throws)
And my javascript:
<script>
function getData() {
fetch('/character')`your text`
.then(response => response.json())
.then(throws => {
document.getElementById("get_data").innerHTML = "You rolled a " +throws;
console.log(throws);
})
}
</script>
<button onclick="getData()">ROLL</button>
<p id="get_data"></p>
Is giving me all the time error: Uncaught (in promise) SyntaxError: Unexpected token '<', "<!doctype "... is not valid JSON
How can I make button which will fetch() value from my function in flask?