A
A
Asya2020-06-14 17:11:12
Python
Asya, 2020-06-14 17:11:12

How to send a POST request correctly?

The POST request
is not always sent either nothing comes or None as in this case:
html

<form action="" method="post" name="sentmol" style="display: none" value="{{ sendit }}"></form>

javascript (here I wait for the pre tag to load)
var checkExist = setInterval(function() {
   if (document.querySelector("#output > pre")) {
      console.log(document.querySelector("#output > pre").innerHTML);
      var sendit = document.querySelector("#output > pre").innerHTML;
          var xhr = new XMLHttpRequest()
    xhr.open("POST", '/', true)
      xhr.send(sendit);
      clearInterval(checkExist);
   }
}, 100);

python
@app.route('/create', methods=['GET', 'POST'])
def create():
   if request.method == 'POST':
        new = request.json
        print('my data = ', new)

output:
my data = None

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergei Chamkin, 2020-06-14
@asyaevloeva

example for passing over JSON
Request:
5ee640da06f84064773097.png
Server-side implementation:

@app.route("/", methods=['GET', 'POST'])
def index():
  if request.method == "POST":
    name = request.json
    print(name)
  return "Hello"

If you need to transfer not json, but text, an example can be found here
https://gist.github.com/KentaYamada/2eed4af1f6b2ad...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question