0

I am trying to send data from javascript to flask when the user downloaded the file,

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>

Flask

@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'))

This is not working, the ajax is not sending the data, help me to correct this, Is there any meta tag to add in head tag like connecting to ajax/jQuery link?

thanks

UPDATING ERROR -

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
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

You should import JQuery (either a hoster version or your own hosted file):

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

Check also this.

miquelvir
  • 1,748
  • 1
  • 7
  • 21