1

I have a page with a ton of input boxes (numbers, checkboxes etc.), I need to prepare the data before the POST request. So I have a bunch of methods that make adjustments to the names of these inputs, bundle them up nicely in one object called: data. For my purposes, putting the submit button in the above form does not work, because the POST request is done with the default names. For that reason all the above inputs are enclosed in a div, and then I have a submit button and onClick I am doing an axios POST request:

axios({
            method: 'post',
            url: '/smart-beta/',
            data
        });

On the Flask end I have this:

    elif request.method == "POST":

      sbe_args = json.loads(request.data)
      sb = SbeGenerator(sbe_args)
      sb.run()  # TODO: depending on how long this takes, may add a loading animation to the page
      eg = ExcelGenerator(sb)
      eg.create_excel()

      beta = sb.beta
      sharpe = sb.sharpe
      annualised_return = sb.ann_mean
      annualised_vol = sb.ann_vol
      time_series = sb.port_json
      stocks = sb.last_stocks_json
      print("Time series: " + str(time_series))
      print("stocks: " + str(stocks))

    # TODO: Do the redirect here (getting 302 status on the POST request if redirect is here, becasue
    # TODO: axios catches it in then
      print("Rendering template")
      return render_template('smart_beta/calculation.html')

I get the 200 on the POST request, but it never renders my other page. It just hangs on the current page. Is that because the axios's promise is never resolved? when I do .then(function(response) {console.log(response);}); <- it still hangs. The reason I need to use render_template is because I need to pass stuff like time_series into that page.

EDIT: When I navigate to the response of the POST request in the dev tools on the web page, I can see that it is the page that I need.

Naz
  • 2,012
  • 1
  • 21
  • 45
  • My guess is that currently the Axios post is getting handled as an AJAX call instead of a form submission. A form submission traditionally expects the server to respond with a new page, where an AJAX requests expects to just receive a packet of data from the server. – jaredkwright Mar 20 '18 at 16:03
  • hmmm. Thanks! How would I handle data preparation then with a form, i.e. I need my method to be ran before the actual submit happens, so that I get a pre-defined object that I then submit – Naz Mar 20 '18 at 16:06
  • https://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit?rq=1 Here is a method where you can create a pseudo-form in Javascript and trigger its submit action. It works by creating form fields under the hood that are never actually rendered out in html. – jaredkwright Mar 20 '18 at 16:09
  • https://stackoverflow.com/a/43014086/3833591 and https://developer.mozilla.org/en-US/docs/Web/API/FormData may also be useful to you – jaredkwright Mar 20 '18 at 16:12
  • thanks. checking out both – Naz Mar 20 '18 at 16:13
  • so the first one is not gonna work because you have to set type to hidden. And I have a couple of inputs that share the same name, but have different types: checkbox and number. Plus, you cannot achieve the structure of something like: "name: [array of values]". Someone suggested FormData class in that answer as well, that won't work too. Does not render a new page on the Flask side. – Naz Mar 20 '18 at 17:18
  • oh and it looks like your second link is precisely about FormData – Naz Mar 20 '18 at 17:18

0 Answers0