I'm having trouble getting Flask to add text to an HTML page after a POST request. The POST returns a 200 code indicating that it went through and I see it in the terminal when the button is clicked. Additionally, I see the request.data printed when the button is clicked so it appears that the python code enters the if statement for the 'POST' request but it never renders the template with the result text. Any ideas?
HTML:
<button onclick="do_ajax()">Do Ajax</button>
<p>{{result}}</p>
JavaScript:
function do_ajax() {
var req = new XMLHttpRequest();
req.open("POST", "/index/", true);
var blob = new Blob(['abc123'], {type: 'text/plain'});
req.send(blob);
}
Flask/ Python:
@app.route('/<string:index>/', methods=['GET','POST'])
def my_form_post(index):
if request.method == 'POST':
print request.data
return render_template('%s.html' % index, result = "It's working!")
elif request.method == 'GET':
return render_template('%s.html' % index)