Using Flask and AJAX I build a HTML table like this...
app.py
tableDict = buildTable()
return render_template('page.html', tableDict = tableDict)
page.html
<tbody>
{% for key, value in tableDict.iteritems() %}
<tr>
<th> {{ key }} </th>
<td> {{ value }} </td>
</tr>
{% endfor %}
</tbody>
That works fine.
I want to update this table using AJAX. I know how to update an element by it's ID tag with something like
ajax.js
$("#elementToUpdate").html(response.valueToUpdate);
So I tried...
app.py
tableDict = buildTable()
return jsonify(tableDict = tableDict)
ajax.js
$("#tableDict").html(response.tableDict);
This doesn't work - my guess is because I'm not updating an ID tag at all...so I shouldn't be surprised about that.
How can I get the dict that I pass to AJAX to update the table?
element.html(html_string)expects html_string to be just that, html, so having your app return json probably won't work. Throw someconsole.log(response.valueToUpdate)statements in there to check what your getting from app.py, then use dev console to check if your jquery selector is actually finding the correct element.