1

I am trying to give data from javascript to python, using post

On my Javascript

$('ABC').on("click", function(){
    $.post( '/user',
        {
            user: 'here'
        }
    )});

And giving this to python

@app.route("/user", methods=['GET', 'POST'])
def user():
    id = user
    return id

Above code should show html with just user.

But page does not move when on click..

Any idea?

11
  • What Python server framework are you using? You need to access the POST parameters; see its documentation. Commented Dec 6, 2018 at 0:02
  • any errors in console? Commented Dec 6, 2018 at 0:02
  • The page doesn't move because you're making an AJAX request. If you want the page to move, you need to submit a POST <form>. Commented Dec 6, 2018 at 0:05
  • @PedroLobito no errors in console.. Commented Dec 6, 2018 at 0:28
  • @ChrisG I know how to use post method in <form> want to know how to do it from ajax Commented Dec 6, 2018 at 0:29

1 Answer 1

1

You need to provide a callback function in $.post

$('ABC').on("click", function() {
  $.post('/user', {
    user: 'here'
  }, function(response) {
    $("#result").html(response);
  });
});

You can also abbreviate this to the .load() method:

$('ABC').on("click", function() {
  $("#result").load('/user', {
    user: 'here'
  });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.