Performing AJAX POST followed by GET with Flask

My goal is for the user to fill out the form, send this information in a POST request to the flash server, and then visualize the template using this form information (after it has some logic on the server).

So far, I have completed the POST part of all of this. I am trying to make a template right now in if request.method == POST' , and I think it is not working right now.

Here is the code that I still have:

 @app.route('/filteredsearch/', methods = ["GET", "POST"]) def filteredsearch(): if request.method == 'POST': data = json.loads(request.data) tables = data['checkboxes'] filter_results = getFilteredEntities(tables = tables) print filter_results #This works return render_template("filteredsearch.html", entities = filter_results) 

Should I make a separate GET request about the success of my POST function? If so, how do I do this?

Here is an AJAX request (if that matters, this code can be called on every page of the application):

 $.ajax({ url:"/filteredsearch/", type: 'POST', data: json, contentType: 'application/json;charset=UTF-8', success: function() { alert("Done"); } }); 

So, ideally, I can display the template at the time of publication. If this is not the case, how do I make a GET request from the same ajax function?

I know that, as a rule, you use url_for() to request a GET , is this parameter given that I am in JS at this point?

+8
jquery python ajax flask
source share
2 answers

You typically use an ajax request to return data that is then dynamically displayed on the page.

 $.ajax({ url:"/filteredsearch/", type: 'POST', data: json, contentType: 'application/json;charset=UTF-8', success: function(evt) { $("#results").html = evt.data; } }); 

If you are going to redirect to another template, then why not just click the "Submit" button in the form that does POST, and then responds with a new template. If you really want to redirect to a new page after the ajax request has returned (which will be slower than just displaying the information already received), you can change this window. location. Something like window.location = 'http://www.example.com' will lead the user to example.com.

+3
source share

I know this a little late, but I had the same problem as you.

I needed to do a POST with Ajax, and then, if it succeeded, I needed to make a GET request for the flask in order to return the processed template. Something like:

FLASK PART:

 @app.route('/something', methods=['POST']) def something_post(): # Get JSON object passed with Ajax request elems = request.json # Do stuff # If everithing is OK, return success message return json.dumps({'success': True}), 200, {'ContentType': 'application/json'} @app.route('/something', methods=['GET']) def something_get(): # Return template return render_template('something.html') 

AJAX PART:

 $.ajax({ type: 'POST', data: myJsonString, // JASON object passed to flask url: '/something', contentType: "application/json", method: 'POST', success: function(response) { # If we succed we make a request to get the template window.location.href = "/something"; # With this line we are making a simple GET request to Flask and the template returned is opened in the current window } }); 

Please note that I am far, far away to become an Ajax expert. I donโ€™t know if this is the right thing to do, but it was the easiest way I found.

+1
source share

All Articles