【问题标题】:Download file and send data to flask using javascript/ajax使用 javascript/ajax 下载文件并将数据发送到烧瓶
【发布时间】:2021-04-06 17:24:26
【问题描述】:

当用户下载文件时,我正在尝试将数据从 javascript 发送到烧瓶,

HTML:

<a href="/static/{{ot[0]}}" download>
    <button type="button" class="btn" position="fixed" bottom="0px" right="0px" onclick="inc('{{ot[0]}}')">Download1</button>
</a>

<script type="text/javascript">
  function inc(argsm){

        var data1 = {'count': 1, 'name':argsm}

        $.ajax({
            type: "POST",
            url:'/test',
            data: JSON.stringify(data1),
            dataType: 'json'

        });
    }

</script>

烧瓶

@app.route('/test', methods=['POST'])
def test():    
    print('test')
    rf=request.form
    print(rf)
    for key in rf.keys():
        data=key
    print(data)
    data_dic=json.loads(data)
    print(data_dic.keys())
    
    return redirect(url_for('home'))

这不起作用,ajax 没有发送数据,帮我纠正这个问题,是否有任何 meta 标签可以添加到 head 标签中,比如连接到 ajax/jQuery 链接?

谢谢

更新错误 -

GET http://127.0.0.1:5000/favicon.ico 404 (NOT FOUND)
home:34 Uncaught TypeError: $.ajax is not a function
    at inc (home:34)
    at HTMLButtonElement.onclick (home:86)
inc @ home:34
onclick @ home:86

【问题讨论】:

  • 您在浏览器控制台中收到任何消息吗? jquery 代码是否触发(尝试打印某些内容)或什至不触发?
  • 我按照你说的检查了控制台日志,它显示-Uncaught TypeError: $.ajax is not a function
  • 有什么解决办法吗...,我什至用了这个标签 -

标签: javascript python ajax flask flask-wtforms


【解决方案1】:

您应该导入 JQuery(主机版本或您自己的托管文件):

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

还要检查this

【讨论】:

  • 我在 head 标签中使用了这个,但显示同样的错误,我也使用引导程序,引导程序是否适用于 ajax?
  • 如果 bootstrap 和 ajax 兼容,那么错误是什么,先生,我是 ajax 新手
  • 我添加了错误跟踪供您参考,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-25
相关资源
最近更新 更多