2

After receiving an Ajax POST by the server I would like to render a specific template but the template isn't rendered, no errors, nothing. I presume it is something to do with receiving a POST?. Any ideas on what the problem could be?

Javascript:

function sendAjax(stringifiedData){
    var csrfToken = document.getElementById("csrf_token").getAttribute("content");
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/completed/');
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.send(stringifiedData);
}

Flask server:

@app.route('/completed/', methods=['POST'])
def completed():
    data = request.json['data']
    logger.debug("data:{0}".format(data)) #This logs OK
    return render_template('index.html')  #Doesn't render
Don Smythe
  • 9,234
  • 14
  • 62
  • 105
  • Where are you checking what the XMLHttpRequest response is? Flask is certainly not ignoring the `render_template()`, but your JavaScript code *does*. – Martijn Pieters Feb 26 '16 at 21:30
  • 1
    In other words, where did you expect the response to turn up? With AJAX requests, the browser will not automatically load the response to replace the current webpage. – Martijn Pieters Feb 26 '16 at 21:33
  • Good question, OK thats good to know. Very difficult finding non JQuery examples I may be forced to use JQuery. – Don Smythe Feb 26 '16 at 21:37
  • 1
    I duped you to a question that shows you how to load the response. – Martijn Pieters Feb 26 '16 at 21:43

0 Answers0