1

I am using a combination of Flask and Javascript. After user input from a web page I send a JSON object back to the Flask server. ie:

var xhr = new XMLHttpRequest();
xhr.open('POST', '/completed/');
xhr.setRequestHeader('Content-Type', 'application/json');
var stringifiedObject = dataResultToJSON(data);
xhr.send(stringifiedObject);

Then in Flask:

@main_view.route('/completed/', methods=['POST'])
def completed():
    if (request.headers['Content-Type'].startswith('application/json')):
        #do stuff here
        return redirect(url_for("main_view.home"))

@main_view.route('/')
def home():
    logger.debug(">>home")
    return render_template('home.html')

When flask redirects to 'home' asfter the Ajax POST I get the following console output:

DEBUG:myapp:>>home
INFO:werkzeug:127.0.0.1 - - [24/Apr/2016 20:13:15] "GET / HTTP/1.1" 200 -
INFO:werkzeug:127.0.0.1 - - [24/Apr/2016 20:13:15] "GET /%3C!DOCTYPE%20html%3E%3C!-- (... entire web page html)

The odd thing is the second INFO statement above - I don't get this line printed when I redirect to home from anywhere else - only occurs when I redirect from the 'completed' POST method. Werkzeug logs the entire home.html web page html and I get an error in the web client:

NetworkError: 404 NOT FOUND - http://127.0.0.1:5000/%3C!DOCTYPE%20html%3E%3C!-- (... entire web page html)

I also added code=307 to the redirect as per here: Make a POST request while redirecting in flask but still got the same 404 error.

I am stuck as to how to get around this.

Community
  • 1
  • 1
Don Smythe
  • 9,234
  • 14
  • 62
  • 105
  • 1
    My belief is that you're using JavaScript to redirect to the result of the POST. This will be the HTML rendered by `home`. You probably just want `completed` to `return url_for('main_view.home')` instead of returning a redirect. – dirn Apr 24 '16 at 17:08

2 Answers2

1

I think your problem is that you're POSTing data as an AJAX request (i.e. not a browser navigation, but programatically from your client). It doesn't really make much sense to tell your AJAX client to redirect after the POST completes.

You're then trying to tell the client to redirect...but the redirect request is being returned to the XMLHttpRequest.

I'm not 100% sure what you want to happen, but you'd probably be better off using a regular form post if you want the client to redirect once you've posted the data.

I believe what you're trying to do is better illustrated by the answer to this question:

How to manage a redirect request after a jQuery Ajax call

Community
  • 1
  • 1
Martin Peck
  • 11,440
  • 2
  • 42
  • 69
1

I got this working following the comment and answer above. Specifically I did:

def completed():
    #other code here
    return url_for("main_view.home")

and in JS:

xhr.onreadystatechange = function() {
    if (xhr.readyState == XMLHttpRequest.DONE) {
        var OK = 200;
        if (xhr.status === OK) {
            window.location.href = xhr.responseText;
        }
        else {
            console.log ('Error: ' + xhr.status); 
        }
    }
};
Don Smythe
  • 9,234
  • 14
  • 62
  • 105