-1

I am trying to send an array from my javascript client to my flask server. I can log the array right before the ajax post-call and see that it exists and looks right. However, when I print it on the flask server-side I receive an empty string.

I have tried 2 different versions of javascript to post it:

Here is the first

$.post("/javamongo", {
                    data: JSON.stringify(data)
                }, function(err, req, resp){
                    window.locationhref = "/results/"+resp["responseJSON"]["uuid"];
                })

here is the second

$.ajax({
    url: '/javamongo',
    type: "POST",
    ContentType: 'application/json',
    data: {'data': data}
    }).done(function(data) {
       console.log(data);
    });

Both are producing different versions of empty when they are received. On my flask server I have tried:

data = request.data

and data = request.data.decode("utf-8")

as well as passing those through JSON.loads() all to no avail.

Any advice or help would be greatly appreciated!

  • I'm not a Flask dev...are you expecting json body being sent? If so you misspelled contentType and would need to stringify the whole data object. Otherwise the $.post version would send as form encoded data the same as submitting an html form with `action="post"` – charlietfl Sep 21 '20 at 22:49
  • First, inspect the request in the browser's dev tools and make sure it's sent correctly. Next, try to log everything on the flask side. Start with `request`. –  Sep 21 '20 at 23:22

1 Answers1

0

Use request.form['data'] instead, since the method you are using is post

Seyi Daniel
  • 2,259
  • 2
  • 8
  • 18