Forums

Flask returning JSON and another value using AJAX

Hi, I'm struggling with a piece of code to return a JSON and a single integer value to my template. I can only access the JSON and the second value doesn't seem to get sent.

@app.route('/make_selection', methods=['GET', 'POST'])
def game():


i = request.args.get('i', type=int)
j = request.args.get('j', type=int)

global FIRST_SELECTED
global CLICK

CLICK= getGemAt(board, i, j)

if CLICK == None and not FIRST_SELECTED:
    return json.dumps(board)

elif CLICK != None and not FIRST_SELECTED:
    FIRST_SELECTED = CLICK
    CLICK = None
    return json.dumps(board), FIRST_SELECTED

elif CLICK != None and FIRST_SELECTED != None:
    return json.dumps(board)

So I'm trying to return the JSON of the board and then the value of the FIRST_SELECTED

Flask does not understand what you want when you return a tuple. If you want to include the FIRST_SELECTED value, add it to the board dictionary before you return the JSON. Then it will be included in the JSON that is returned.

use jsonify instead of json.dumps