使用 ajax 和 jQuery 传递表单和文件数据时出现问题

我想将一些表单数据和文件从表单传递到烧瓶应用程序。但我不能用ajax传递它。我猜数据有问题。我已经在 ajax 中发送数据,但在 flask 应用程序中我没有得到任何字符串或文件。


这是我的 html 代码:



<form id="user_vote" enctype = "multipart/form-data">

        <br>

        <br>

        <div class="row">

            <label class="col-sm-2">Name:</label>

            <div class="col-sm-10">

                <input type="text" name="name" id="name" rows="2" class="form-control" required>

            </div>

        </div>


        <div class="row">

            <label class="col-sm-2">National ID Image:</label>

            <div class="col-sm-10">

                <input type="file" name="national_id_image" id="national_id_image" rows="2" class="form-control" required>

            </div>

        </div>

        <br>


        <div class="row">

            <label class="col-sm-2">Vote:</label>

            <div class="col-sm-10">

                <input type="number" name="vote" id="vote" rows="2" class="form-control" required>

            </div>

        </div>


        <div class="row">

            <div class="col-lg-12 text-center">

                <input type="button" id="submit_vote" class="btn btn-primary btn-lg"

                       value="Authenticate and Encrypt Vote">

            </div>

        </div>


这是我的 ajax 代码::




      $(function(){


        var form = $('#user_vote')[0];

        var data = new FormData(form);


        //console.log('hello');

        //console.log(document.getElementById('submit_vote'));


        $('#submit_vote').click(function(){

          //console.log(data);

          //console.log('hello');


          $.ajax({

            url: '/encrypt/vote',

            type: "POST",

            dataType: 'json',

            enctype: 'multipart/form-data',

            data: data,

            contentType: false,

            cache: false,

            processData:false,

            success: function(response)


千巷猫影
浏览 64回答 1
1回答

蝴蝶不菲

似乎您设置了enctype: 'multipart/form-data', 这是$.ajax() 方法的不存在的属性。您应该更正此错误并简化请求:$.ajax({&nbsp; &nbsp; type: "POST",&nbsp; &nbsp; data: data,&nbsp; &nbsp; url: '/encrypt/vote',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; cache: false,&nbsp; &nbsp; contentType: false,&nbsp; &nbsp; processData: false,&nbsp; &nbsp; success: function(response) {&nbsp; &nbsp; &nbsp; &nbsp; /*The rest of your code*/&nbsp; &nbsp; },&nbsp; &nbsp; error: function(error){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(error);&nbsp; &nbsp; }});无需设置dataType,默认为Intelligent Guess(xml, json, script, or html)。在这里阅读更多。编辑:确保您在 中使用正确的完整路径url,尽量不要使用相对地址,而是使用https://www.your-server.com/encrypt/vote。
打开App,查看更多内容
随时随地看视频慕课网APP