简体   繁体   English

数据从javascript到python(使用post)

[英]Data from javascript to python(using post)

I am trying to give data from javascript to python, using post 我正在尝试使用post将数据从javascript提供给python

On my Javascript 在我的Javascript上

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

And giving this to python 并将其交给python

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

Above code should show html with just user. 上面的代码应仅以用户身份显示html。

But page does not move when on click.. 但是单击时页面不会移动。

Any idea? 任何想法?

You need to provide a callback function in $.post 您需要在$.post提供一个回调函数

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

You can also abbreviate this to the .load() method: 您也可以将其缩写为.load()方法:

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

From what I understand, you're looking for this: 据我了解,您正在寻找:

$('ABC').on("click", function() {
  var $form = $("<form>").attr("action", "/post").attr("method", "post");
  $form.append($("<input>").attr("type", "hidden").attr("name", "user").val("here"));
  $("body").append($form);
  $form.submit();
  return false;
});

This will create a hidden POST form, populate it, append it to <body> and submit it. 这将创建一个隐藏的POST表单,填充它,将其附加到<body>并提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM